material-components/material-components-android · error · IllegalArgumentException
Called setCheckedItem(MenuItem) with an item that is not in
Error message
Called setCheckedItem(MenuItem) with an item that is not in the current menu.
What it means
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).
Source
Thrown at lib/java/com/google/android/material/navigation/NavigationView.java:871
*/
public void setCheckedItem(@IdRes int id) {
MenuItem item = menu.findItem(id);
if (item != null) {
presenter.setCheckedItem((MenuItemImpl) item);
}
}
/**
* Sets the currently checked item in this navigation menu.
*
* @param checkedItem The checked item from the menu available from {@link #getMenu()}.
*/
public void setCheckedItem(@NonNull MenuItem checkedItem) {
MenuItem item = menu.findItem(checkedItem.getItemId());
if (item != null) {
presenter.setCheckedItem((MenuItemImpl) item);
} else {
throw new IllegalArgumentException(
"Called setCheckedItem(MenuItem) with an item that is not in the current menu.");
}
}
/** Returns the currently checked item in this navigation menu. */
@Nullable
public MenuItem getCheckedItem() {
return presenter.getCheckedItem();
}
/**
* Set the text appearance of the menu items to a given resource.
*
* @attr ref R.styleable#NavigationView_itemTextAppearance
*/
public void setItemTextAppearance(@StyleRes int resId) {
presenter.setItemTextAppearance(resId);
}View on GitHub (pinned to ac7e18efee)
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
Example fix
// before navView.setCheckedItem(otherMenu.findItem(R.id.nav_home)) // after navView.setCheckedItem(navView.menu.findItem(R.id.nav_home))
Defensive patterns
Strategy: validation
Validate before calling
fun setCheckedIfExists(navView: NavigationView, itemId: Int): Boolean {
val item = navView.menu.findItem(itemId)
if (item != null) navView.setCheckedItem(item)
return item != null
} Try / catch
catch (e: IllegalArgumentException) { Log.w(TAG, "Item not in this nav menu; skipping", e) } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- %s does not support submenus
- Maximum number of items supported by %s is %d. Limit can be
- Only one layer of submenu is supported; a submenu inside a s
- %d is not a valid view id
- NavigationView back progress requires the direct parent view
AI-assisted analysis of material-components/material-components-android@ac7e18efee (2026-08-14).
Data as JSON: /api/errors/3a788ed8923f0043.
Report an issue: GitHub.