H07000223/FlycoTabLayout · error · IllegalStateException

ViewPager can not be NULL !

Error message

ViewPager can not be NULL !

What it means

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.

Solutions

  1. Ensure the ViewPager view exists (setContentView/inflate before binding) and pass the non-null reference.
  2. Fix the view ID / binding lookup so it resolves to the actual ViewPager in the layout.
  3. If you already have an adapter, use setViewPager(vp) or setViewPager(vp, titles) instead.
  4. Null-check and early-return or log before calling if the view may legitimately be absent.

Example fix

// before
slidingTabLayout.setViewPager(null, titles, this, fragments);
// after
ViewPager vp = findViewById(R.id.viewpager);
if (vp != null) {
    slidingTabLayout.setViewPager(vp, titles, this, fragments);
}
Defensive patterns

Strategy: validation

Validate before calling

ViewPager vp = findViewById(R.id.viewpager);
if (vp != null && titles != null && titles.length > 0 && titles.length == fragments.size()) {
    slidingTabLayout.setViewPager(vp, titles, this, fragments);
}

Type guard

boolean canBindConvenience(ViewPager vp, String[] titles, ArrayList<Fragment> fragments) {
    return vp != null && titles != null && titles.length > 0
        && fragments != null && titles.length == fragments.size();
}

Try / catch

try {
    slidingTabLayout.setViewPager(vp, titles, this, fragments);
} catch (IllegalStateException e) {
    Log.e("SlidingTabLayout", "ViewPager null", e);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of H07000223/FlycoTabLayout@528fcffe88 (2026-09-08). Data as JSON: /api/errors/7e20119411b962d6. Report an issue: GitHub.

Appendix: source

Thrown at FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/SlidingTabLayout.java:211

        }

        if (titles.length != vp.getAdapter().getCount()) {
            throw new IllegalStateException("Titles length must be the same as the page count !");
        }

        this.mViewPager = vp;
        mTitles = new ArrayList<>();
        Collections.addAll(mTitles, titles);

        this.mViewPager.removeOnPageChangeListener(this);
        this.mViewPager.addOnPageChangeListener(this);
        notifyDataSetChanged();
    }

    /** 关联ViewPager,用于连适配器都不想自己实例化的情况 */
    public void setViewPager(ViewPager vp, String[] titles, FragmentActivity fa, ArrayList<Fragment> fragments) {
        if (vp == null) {
            throw new IllegalStateException("ViewPager can not be NULL !");
        }

        if (titles == null || titles.length == 0) {
            throw new IllegalStateException("Titles can not be EMPTY !");
        }

        this.mViewPager = vp;
        this.mViewPager.setAdapter(new InnerPagerAdapter(fa.getSupportFragmentManager(), fragments, titles));

        this.mViewPager.removeOnPageChangeListener(this);
        this.mViewPager.addOnPageChangeListener(this);
        notifyDataSetChanged();
    }

    /** 更新数据 */
    public void notifyDataSetChanged() {
        mTabsContainer.removeAllViews();
        this.mTabCount = mTitles == null ? mViewPager.getAdapter().getCount() : mTitles.size();

View on GitHub (pinned to 528fcffe88)