{"record":{"id":"879d1aadd340e766","repo":"material-components/material-components-android","slug":"tab-belongs-to-a-different-tablayout","errorCode":null,"errorMessage":"Tab belongs to a different TabLayout.","messagePattern":"Tab belongs to a different TabLayout\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"lib/java/com/google/android/material/tabs/TabLayout.java","lineNumber":866,"sourceCode":"   * Add a tab to this layout. The tab will be added at the end of the list.\n   *\n   * @param tab Tab to add\n   * @param setSelected True if the added tab should become the selected tab.\n   */\n  public void addTab(@NonNull Tab tab, boolean setSelected) {\n    addTab(tab, tabs.size(), setSelected);\n  }\n\n  /**\n   * Add a tab to this layout. The tab will be inserted at <code>position</code>.\n   *\n   * @param tab The tab to add\n   * @param position The new position of the tab\n   * @param setSelected True if the added tab should become the selected tab.\n   */\n  public void addTab(@NonNull Tab tab, int position, boolean setSelected) {\n    if (tab.parent != this) {\n      throw new IllegalArgumentException(\"Tab belongs to a different TabLayout.\");\n    }\n    configureTab(tab, position);\n    addTabView(tab);\n\n    if (setSelected) {\n      tab.select();\n    }\n  }\n\n  private void addTabFromItemView(@NonNull TabItem item) {\n    final Tab tab = newTab();\n    if (item.text != null) {\n      tab.setText(item.text);\n    }\n    if (item.icon != null) {\n      tab.setIcon(item.icon);\n    }\n    if (item.customLayout != 0) {","sourceCodeStart":848,"sourceCodeEnd":884,"githubUrl":"https://github.com/material-components/material-components-android/blob/ac7e18efeefb331850c561faf9ab8bf81d27ba68/lib/java/com/google/android/material/tabs/TabLayout.java#L848-L884","documentation":"TabLayout.Tab objects are created by and owned by a specific TabLayout (tab.parent is set in newTab()). addTab(tab, position, setSelected) throws IllegalArgumentException if the tab was created by a different TabLayout instance, because tab views and position bookkeeping are per-layout.","triggerScenarios":"Creating a tab with tabLayout1.newTab() and adding it via tabLayout2.addTab(tab) or tabLayout2.addTab(tab, position, true).","commonSituations":"Two TabLayouts in one screen (top + bottom, or tabs + navigation) and copy-pasted setup code; recreating tabs after a configuration change using the wrong saved TabLayout reference; helper methods that take the wrong TabLayout parameter.","solutions":["Create every tab from the same TabLayout that will display it: tabLayout.addTab(tabLayout.newTab().setText(...)).","Audit helper/factory methods to make sure the TabLayout used for newTab() is the one used for addTab().","If tabs move between layouts, remove from the original layout and recreate with newTab() on the target layout."],"exampleFix":"// before\nTab tab = topTabs.newTab().setText(\"A\");\nbottomTabs.addTab(tab, 0, true); // throws\n\n// after\nbottomTabs.addTab(bottomTabs.newTab().setText(\"A\"), 0, true);","handlingStrategy":"validation","validationCode":"// Tab ownership has no public getter; enforce it structurally:\nprivate static void addTabSafe(@NonNull TabLayout layout, @NonNull CharSequence text) {\n  layout.addTab(layout.newTab().setText(text)); // same layout creates AND adds\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Create and add tabs with the same TabLayout reference in one method.","Never cache Tab objects across different TabLayouts.","With multiple TabLayouts, name references per layout (topTabs/bottomTabs) to avoid mix-ups."],"tags":["tablayout","tabs","android","ui"],"backgroundTag":null,"analyzedSha":"ac7e18efeefb331850c561faf9ab8bf81d27ba68","analyzedAt":"2026-08-14T15:35:35.735Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}