{"record":{"id":"af78de8c6a502bf6","repo":"material-components/material-components-android","slug":"d-is-not-a-valid-view-id","errorCode":null,"errorMessage":"%d is not a valid view id","messagePattern":"(.+?) is not a valid view id","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"lib/java/com/google/android/material/navigation/NavigationBarMenuView.java","lineNumber":1511,"sourceCode":"  @NonNull\n  protected abstract NavigationBarItemView createNavigationBarItemView(@NonNull Context context);\n\n  protected int getSelectedItemPosition() {\n    return selectedItemPosition;\n  }\n\n  @Nullable\n  protected NavigationBarMenuBuilder getMenu() {\n    return menu;\n  }\n\n  private boolean isValidId(int viewId) {\n    return viewId != View.NO_ID;\n  }\n\n  private void validateMenuItemId(int viewId) {\n    if (!isValidId(viewId)) {\n      throw new IllegalArgumentException(viewId + \" is not a valid view id\");\n    }\n  }\n\n  public void updateActiveIndicator(int availableWidth) {\n    if (buttons != null) {\n      for (NavigationBarMenuItemView item : buttons) {\n        if (item instanceof NavigationBarItemView) {\n          ((NavigationBarItemView) item).updateActiveIndicatorLayoutParams(availableWidth);\n        }\n      }\n    }\n  }\n}\n","sourceCodeStart":1493,"sourceCodeEnd":1525,"githubUrl":"https://github.com/material-components/material-components-android/blob/ac7e18efeefb331850c561faf9ab8bf81d27ba68/lib/java/com/google/android/material/navigation/NavigationBarMenuView.java#L1493-L1525","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nval item = bottomNav.menu.add(\"Profile\")\n// after\nval item = bottomNav.menu.add(0, R.id.nav_profile, Menu.NONE, \"Profile\")","handlingStrategy":"validation","validationCode":"fun isValidItemId(id: Int): Boolean = id != View.NO_ID && id != MenuItem.NONE","typeGuard":null,"tryCatchPattern":"catch (e: IllegalArgumentException) { Log.e(TAG, \"Menu item has no valid id\", e); assignIdsAndRetry() }","preventionTips":["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"],"tags":["android","material-components","navigation","menu","view-id"],"backgroundTag":null,"analyzedSha":"ac7e18efeefb331850c561faf9ab8bf81d27ba68","analyzedAt":"2026-08-14T15:35:35.735Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}