{"record":{"id":"7e20119411b962d6","repo":"H07000223/FlycoTabLayout","slug":"viewpager-can-not-be-null","errorCode":null,"errorMessage":"ViewPager can not be NULL !","messagePattern":"ViewPager can not be NULL !","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/SlidingTabLayout.java","lineNumber":211,"sourceCode":"        }\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) {\n            throw new IllegalStateException(\"Titles can not be EMPTY !\");\n        }\n\n        this.mViewPager = vp;\n        this.mViewPager.setAdapter(new InnerPagerAdapter(fa.getSupportFragmentManager(), fragments, titles));\n\n        this.mViewPager.removeOnPageChangeListener(this);\n        this.mViewPager.addOnPageChangeListener(this);\n        notifyDataSetChanged();\n    }\n\n    /** 更新数据 */\n    public void notifyDataSetChanged() {\n        mTabsContainer.removeAllViews();\n        this.mTabCount = mTitles == null ? mViewPager.getAdapter().getCount() : mTitles.size();","sourceCodeStart":193,"sourceCodeEnd":229,"githubUrl":"https://github.com/H07000223/FlycoTabLayout/blob/528fcffe88d54eeafbc85ffd1ce375df043b3a28/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/SlidingTabLayout.java#L193-L229","documentation":"The four-argument SlidingTabLayout.setViewPager(ViewPager, String[], FragmentActivity, ArrayList<Fragment>) throws IllegalStateException when the ViewPager is null. This variant builds its own InnerPagerAdapter from the given activity's FragmentManager, but it still requires an existing ViewPager to attach to.","triggerScenarios":"Passing null as the first argument to the 4-arg setViewPager; the ViewPager lookup (findViewById/ViewBinding) returned null because the layout wasn't inflated or the ID is wrong.","commonSituations":"Using the convenience overload inside a Fragment with an Activity-typed context mistake or before view creation; typo'd view ID in layout XML; calling from a secondary thread or before setContentView.","solutions":["Ensure the ViewPager view exists (setContentView/inflate before binding) and pass the non-null reference.","Fix the view ID / binding lookup so it resolves to the actual ViewPager in the layout.","If you already have an adapter, use setViewPager(vp) or setViewPager(vp, titles) instead.","Null-check and early-return or log before calling if the view may legitimately be absent."],"exampleFix":"// before\nslidingTabLayout.setViewPager(null, titles, this, fragments);\n// after\nViewPager vp = findViewById(R.id.viewpager);\nif (vp != null) {\n    slidingTabLayout.setViewPager(vp, titles, this, fragments);\n}","handlingStrategy":"validation","validationCode":"ViewPager vp = findViewById(R.id.viewpager);\nif (vp != null && titles != null && titles.length > 0 && titles.length == fragments.size()) {\n    slidingTabLayout.setViewPager(vp, titles, this, fragments);\n}","typeGuard":"boolean canBindConvenience(ViewPager vp, String[] titles, ArrayList<Fragment> fragments) {\n    return vp != null && titles != null && titles.length > 0\n        && fragments != null && titles.length == fragments.size();\n}","tryCatchPattern":"try {\n    slidingTabLayout.setViewPager(vp, titles, this, fragments);\n} catch (IllegalStateException e) {\n    Log.e(\"SlidingTabLayout\", \"ViewPager null\", e);\n}","preventionTips":["Call setContentView/inflate before resolving the ViewPager.","Double-check the ViewPager ID in layout XML.","Use ViewBinding to eliminate null findViewById results.","Prefer the explicit-adapter overloads when the ViewPager lifecycle is complex."],"tags":["android","viewpager","tablayout","null-check"],"backgroundTag":"null-argument","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"}