{"record":{"id":"3a788ed8923f0043","repo":"material-components/material-components-android","slug":"called-setcheckeditem-menuitem-with-an-item-that","errorCode":null,"errorMessage":"Called setCheckedItem(MenuItem) with an item that is not in the current menu.","messagePattern":"Called setCheckedItem\\(MenuItem\\) with an item that is not in the current menu\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"lib/java/com/google/android/material/navigation/NavigationView.java","lineNumber":871,"sourceCode":"   */\n  public void setCheckedItem(@IdRes int id) {\n    MenuItem item = menu.findItem(id);\n    if (item != null) {\n      presenter.setCheckedItem((MenuItemImpl) item);\n    }\n  }\n\n  /**\n   * Sets the currently checked item in this navigation menu.\n   *\n   * @param checkedItem The checked item from the menu available from {@link #getMenu()}.\n   */\n  public void setCheckedItem(@NonNull MenuItem checkedItem) {\n    MenuItem item = menu.findItem(checkedItem.getItemId());\n    if (item != null) {\n      presenter.setCheckedItem((MenuItemImpl) item);\n    } else {\n      throw new IllegalArgumentException(\n          \"Called setCheckedItem(MenuItem) with an item that is not in the current menu.\");\n    }\n  }\n\n  /** Returns the currently checked item in this navigation menu. */\n  @Nullable\n  public MenuItem getCheckedItem() {\n    return presenter.getCheckedItem();\n  }\n\n  /**\n   * Set the text appearance of the menu items to a given resource.\n   *\n   * @attr ref R.styleable#NavigationView_itemTextAppearance\n   */\n  public void setItemTextAppearance(@StyleRes int resId) {\n    presenter.setItemTextAppearance(resId);\n  }","sourceCodeStart":853,"sourceCodeEnd":889,"githubUrl":"https://github.com/material-components/material-components-android/blob/ac7e18efeefb331850c561faf9ab8bf81d27ba68/lib/java/com/google/android/material/navigation/NavigationView.java#L853-L889","documentation":"NavigationView.setCheckedItem(MenuItem) looks the item up in the view's own menu by id and throws IllegalArgumentException when findItem returns null — the passed MenuItem does not belong to this NavigationView's menu (different menu instance, item never added, or stale item from another view).","triggerScenarios":"Passing a MenuItem obtained from a different NavigationView or a PopupMenu; passing an item built before the nav menu was (re)inflated; calling setCheckedItem(item) where item.getItemId() has no match in the current menu.","commonSituations":"Two NavigationViews (start/end drawers) sharing handling code; checking an item by an id not present in the menu XML; keeping a cached MenuItem across menu re-inflation.","solutions":["Look the item up from the same view first: navView.setCheckedItem(navView.getMenu().findItem(id)) or use setCheckedItem(int id) semantics","Ensure the item id exists in the menu XML used by that NavigationView","Never reuse MenuItem instances across different menu/view instances"],"exampleFix":"// before\nnavView.setCheckedItem(otherMenu.findItem(R.id.nav_home))\n// after\nnavView.setCheckedItem(navView.menu.findItem(R.id.nav_home))","handlingStrategy":"validation","validationCode":"fun setCheckedIfExists(navView: NavigationView, itemId: Int): Boolean {\n    val item = navView.menu.findItem(itemId)\n    if (item != null) navView.setCheckedItem(item)\n    return item != null\n}","typeGuard":null,"tryCatchPattern":"catch (e: IllegalArgumentException) { Log.w(TAG, \"Item not in this nav menu; skipping\", e) }","preventionTips":["Always resolve the item from the same view's getMenu() before setCheckedItem","Verify the id exists in the inflated menu XML","Never cache MenuItem instances across views"],"tags":["android","material-components","navigation","navigationview","menu"],"backgroundTag":null,"analyzedSha":"ac7e18efeefb331850c561faf9ab8bf81d27ba68","analyzedAt":"2026-08-14T15:35:35.735Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}