{"record":{"id":"411d8629a333e4db","repo":"H07000223/FlycoTabLayout","slug":"titles-length-must-be-the-same-as-the-page-count","errorCode":null,"errorMessage":"Titles length must be the same as the page count !","messagePattern":"Titles length must be the same as the page count !","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/SlidingTabLayout.java","lineNumber":196,"sourceCode":"        this.mViewPager = vp;\n\n        this.mViewPager.removeOnPageChangeListener(this);\n        this.mViewPager.addOnPageChangeListener(this);\n        notifyDataSetChanged();\n    }\n\n    /** 关联ViewPager,用于不想在ViewPager适配器中设置titles数据的情况 */\n    public void setViewPager(ViewPager vp, String[] titles) {\n        if (vp == null || vp.getAdapter() == null) {\n            throw new IllegalStateException(\"ViewPager or ViewPager adapter can not be NULL !\");\n        }\n\n        if (titles == null || titles.length == 0) {\n            throw new IllegalStateException(\"Titles can not be EMPTY !\");\n        }\n\n        if (titles.length != vp.getAdapter().getCount()) {\n            throw new IllegalStateException(\"Titles length must be the same as the page count !\");\n        }\n\n        this.mViewPager = vp;\n        mTitles = new ArrayList<>();\n        Collections.addAll(mTitles, titles);\n\n        this.mViewPager.removeOnPageChangeListener(this);\n        this.mViewPager.addOnPageChangeListener(this);\n        notifyDataSetChanged();\n    }\n\n    /** 关联ViewPager,用于连适配器都不想自己实例化的情况 */\n    public void setViewPager(ViewPager vp, String[] titles, FragmentActivity fa, ArrayList<Fragment> fragments) {\n        if (vp == null) {\n            throw new IllegalStateException(\"ViewPager can not be NULL !\");\n        }\n\n        if (titles == null || titles.length == 0) {","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/H07000223/FlycoTabLayout/blob/528fcffe88d54eeafbc85ffd1ce375df043b3a28/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/SlidingTabLayout.java#L178-L214","documentation":"SlidingTabLayout.setViewPager(ViewPager, String[]) also verifies that titles.length equals vp.getAdapter().getCount(); a mismatch throws IllegalStateException \"Titles length must be the same as the page count !\". Tab count and page count must stay 1:1 or clicks/scrolls will map to the wrong pages.","triggerScenarios":"Supplying more or fewer titles than the adapter's page count, e.g. adding a fragment to the adapter without updating the titles array, or filtering one list but not the other.","commonSituations":"Dynamic adapters whose count changes after initial setup; hardcoded titles arrays that drifted from the fragment list; merging branches where one side added a page only.","solutions":["Update the titles array so its length matches adapter.getCount(), then re-call setViewPager.","Better: implement getPageTitle() in the PagerAdapter and use the 1-arg setViewPager so titles live in one place.","On adapter data changes, rebuild titles and rebind the tab layout in the same code path.","Add an assertion/unit test comparing titles.length to adapter.getCount() before binding."],"exampleFix":"// before\nString[] titles = {\"A\", \"B\"}; // adapter has 3 pages\nslidingTabLayout.setViewPager(viewPager, titles);\n// after\nString[] titles = {\"A\", \"B\", \"C\"}; // matches adapter.getCount()\nslidingTabLayout.setViewPager(viewPager, titles);","handlingStrategy":"validation","validationCode":"if (vp.getAdapter() != null && titles != null && titles.length == vp.getAdapter().getCount()) {\n    slidingTabLayout.setViewPager(vp, titles);\n}","typeGuard":"boolean titlesMatchPages(ViewPager vp, String[] titles) {\n    return vp.getAdapter() != null && titles != null\n        && titles.length == vp.getAdapter().getCount();\n}","tryCatchPattern":"try {\n    slidingTabLayout.setViewPager(vp, titles);\n} catch (IllegalStateException e) {\n    Log.e(\"SlidingTabLayout\", \"titles/page count mismatch\", e);\n}","preventionTips":["Update adapter and titles arrays together in one code path.","Implement PagerAdapter.getPageTitle() so counts cannot diverge.","Rebind the tab layout whenever adapter data changes.","Add a debug assertion comparing titles.length to adapter.getCount()."],"tags":["android","tablayout","titles","adapter","count-mismatch"],"backgroundTag":"invalid-argument-value","analyzedSha":"528fcffe88d54eeafbc85ffd1ce375df043b3a28","analyzedAt":"2026-09-08T23:52:16.457Z","contentChangedAt":"2026-09-08T23:52:16.457Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}