{"record":{"id":"7ad42adb028e1489","repo":"H07000223/FlycoTabLayout","slug":"titles-can-not-be-empty","errorCode":null,"errorMessage":"Titles can not be EMPTY !","messagePattern":"Titles can not be EMPTY !","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/SlidingTabLayout.java","lineNumber":192,"sourceCode":"        if (vp == null || vp.getAdapter() == null) {\n            throw new IllegalStateException(\"ViewPager or ViewPager adapter can not be NULL !\");\n        }\n\n        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) {","sourceCodeStart":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/H07000223/FlycoTabLayout/blob/528fcffe88d54eeafbc85ffd1ce375df043b3a28/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/SlidingTabLayout.java#L174-L210","documentation":"In SlidingTabLayout.setViewPager(ViewPager, String[]), a null or zero-length titles array throws IllegalStateException \"Titles can not be EMPTY !\". Each tab needs a title string; an empty array would leave the tab strip blank and desynchronized from the adapter.","triggerScenarios":"Passing titles == null or titles.length == 0 to the two-argument setViewPager; passing an array built from an empty list or missing string resources.","commonSituations":"Titles loaded asynchronously and not yet populated when setViewPager runs; localized string arrays resolving to nothing; copy-paste of the 1-arg setViewPager call without adding titles.","solutions":["Pass a titles array with exactly one non-null entry per page.","Validate titles != null && titles.length > 0 before the call, and also match vp.getAdapter().getCount() (a later check throws otherwise).","Defer setViewPager until titles are loaded, or use the 1-arg setViewPager with an adapter that supplies titles via getPageTitle().","Catch IllegalStateException when empty titles can legitimately occur at startup."],"exampleFix":"// before\nslidingTabLayout.setViewPager(viewPager, new String[0]);\n// after\nString[] titles = {\"Tab 1\", \"Tab 2\"};\nslidingTabLayout.setViewPager(viewPager, titles);","handlingStrategy":"validation","validationCode":"if (titles != null && titles.length > 0 && titles.length == vp.getAdapter().getCount()) {\n    slidingTabLayout.setViewPager(vp, titles);\n}","typeGuard":"boolean isValidTitleSet(ViewPager vp, String[] titles) {\n    return titles != null && titles.length > 0\n        && vp != null && vp.getAdapter() != null\n        && titles.length == vp.getAdapter().getCount();\n}","tryCatchPattern":"try {\n    slidingTabLayout.setViewPager(vp, titles);\n} catch (IllegalStateException e) {\n    Log.e(\"SlidingTabLayout\", \"titles null/empty\", e);\n}","preventionTips":["Derive titles from the same collection that feeds the adapter.","Defer binding until both pages and titles are ready.","Never pass new String[0] placeholders to production code paths.","Write a test asserting titles.length == adapter.getCount()."],"tags":["android","tablayout","titles","validation"],"backgroundTag":"empty-required-field","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"}