material-components/material-components-android · error · IllegalArgumentException
%d is not a valid view id
Error message
%d is not a valid view id
What it means
validateMenuItemId throws IllegalArgumentException when a menu item id equals View.NO_ID (-1). Several NavigationBarMenuView APIs that operate on a specific item (checked item, badge, etc.) need a real id to look items up, so the guard rejects the sentinel 'no id' value before doing any work.
Source
Thrown at lib/java/com/google/android/material/navigation/NavigationBarMenuView.java:1511
@NonNull
protected abstract NavigationBarItemView createNavigationBarItemView(@NonNull Context context);
protected int getSelectedItemPosition() {
return selectedItemPosition;
}
@Nullable
protected NavigationBarMenuBuilder getMenu() {
return menu;
}
private boolean isValidId(int viewId) {
return viewId != View.NO_ID;
}
private void validateMenuItemId(int viewId) {
if (!isValidId(viewId)) {
throw new IllegalArgumentException(viewId + " is not a valid view id");
}
}
public void updateActiveIndicator(int availableWidth) {
if (buttons != null) {
for (NavigationBarMenuItemView item : buttons) {
if (item instanceof NavigationBarItemView) {
((NavigationBarItemView) item).updateActiveIndicatorLayoutParams(availableWidth);
}
}
}
}
}
View on GitHub (pinned to ac7e18efee)
Solutions
- Give every menu item a stable id: XML android:id, or menu.add(groupId, itemId, order, title)
- Check itemId != View.NO_ID (and != MenuItem.NONE) before calling id-based APIs
- Keep item ids in a constants object or @IdRes to avoid magic numbers
Example fix
// before
val item = bottomNav.menu.add("Profile")
// after
val item = bottomNav.menu.add(0, R.id.nav_profile, Menu.NONE, "Profile") Defensive patterns
Strategy: validation
Validate before calling
fun isValidItemId(id: Int): Boolean = id != View.NO_ID && id != MenuItem.NONE
Try / catch
catch (e: IllegalArgumentException) { Log.e(TAG, "Menu item has no valid id", e); assignIdsAndRetry() } Prevention
- Always assign ids when adding items: menu.add(group, id, order, title)
- Define nav item ids as @IdRes constants
- Validate ids from external data before touching menu APIs
When it happens
Trigger: Calling NavigationBarMenuView methods that route through validateMenuItemId (e.g. setting a badge or checked state by id) with an item that was added without an id, or with the literal -1; menu items created via menu.add("title") without specifying an id get NO_ID.
Common situations: Programmatically adding menu items with menu.add(title) (no id) and then trying to reference them by id; passing MenuItem.NO_ID or 0/-1 constants by mistake.
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
- Called setCheckedItem(MenuItem) with an item that is not in
- secondaryIconGravity cannot have the same alignment as iconG
AI-assisted analysis of material-components/material-components-android@ac7e18efee (2026-08-14).
Data as JSON: /api/errors/af78de8c6a502bf6.
Report an issue: GitHub.