IllegalArgumentException: No view found for id for fragment --- ViewPager in ViewPager

Update:

I've read the source code of the FragmentManager and finally got the true reason: This exception happens when the fragments want to be attached to the viewpager before the viewpager is attached to its parent. In other words, before the getView() method returns, the fragments are inflated. Then the findViewById() method of the container of the ViewPager is called but the ViewPager is in detached state yet, so null is found and the IllegalArgumentException is thrown.

The solution is to create a custom ViewPager and lazy set the adapter:

public class BannerViewPager extends ViewPager {
    PagerAdapter mPagerAdapter;

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (mPagerAdapter != null) {
            super.setAdapter(mPagerAdapter);
            mPageIndicator.setViewPager(this);
        }
    }

    @Override
    public void setAdapter(PagerAdapter adapter) {
    }

    public void storeAdapter(PagerAdapter pagerAdapter) {
        mPagerAdapter = pagerAdapter;
    }

    public BannerViewPager(Context context) {
        super(context);
    }

    public BannerViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

}

And in the getView() method, use storeAdapter() instead of setAdapter.

The following statements are not correct. The words above are the actual reason.


Finally I've got the answer. It consists of two parts.

  1. In the parent ViewPager I used a FragmentPagerAdapter to hold fragments, but now I use a FragmentStatePagerAdapter instead. The difference between these two can be found here: Difference between FragmentPagerAdapter and FragmentStatePagerAdapter.
    Simply speaking, FragmentPagerAdapter will store more information when a fragment is stopped. In this situation, the first fragment in the parent ViewPager is stopped but not destroyed while the views in this fragment are destroyed. After resumed, the fragment try to re-inflate all the views. But before the getView() method is called and the sub-ViewPager is recreated, the child FragmentManager tries to find the sub-ViewPager to hold those previously stored fragments. Thus the "java.lang.IllegalArgumentException: No view found for id" occurs.

  2. After I replaced the FragmentPagerAdapter with FragmentStatePagerAdapter, another problem appears. the sub-viewpager is missing when the parent fragment (the first fragment in the parent viewpager) was stopped, destroyed and resumed. This happens when the first fragment is choosen, soon afterwards the third fragment is choosen, and at last the first fragment is re-choosen.
    I think this is a bug of android sdk. Inspired by here and here, I use some tricky methods to solve the problem. The point is, when a parent fragment is destroyed, the field member --- mChildFragmentManager "ends up with a broken internal state" and is not totally cleaned up. When the parent fragment is recreated, mChildFragmentManager is not null, but the sub fragments is already been destroyed after the parent fragment being destroyed, which was managered by mChildFragmentManager. Thus the sub ViewPager displays an empty view on the screen, which responds to a fake fragment which is actually not existing. The funny thing is, after swiping right on the sub ViewPager for several times, the sub fragments and the views appear again.

Here is the codes:

Parent Adapter:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = getBannerView(mParent);
    }
    mViewPager = (ViewPager) convertView;
    if (mBanners != null && !mBanners.isEmpty()) {
        if (mPagerAdapter == null) {
            FragmentManager childFM = mFragment.getChildFragmentManager();
            removeOldFragment(childFM);
            mPagerAdapter = new ScreenSlidePagerAdapter(childFM, mBanners);
            mViewPager.setAdapter(mPagerAdapter);
        }
    }
    return convertView;
}

The key method:

    private void removeOldFragment(FragmentManager fm) {
        try {
            Field added = fm.getClass().getDeclaredField("mAdded");
            added.setAccessible(true);
            added.set(fm, null);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        try {
            Field active = fm.getClass().getDeclaredField("mActive");
            active.setAccessible(true);
            active.set(fm, null);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

Xieyi did a good job of explaining the reasoning behind the exception.

Here's the solution I found:

@Override
public void onPause() {
    super.onPause();

    for ( Fragment f : getChildFragmentManager().getFragments() ) {
        if ( f instanceof MyFragmentType ) {
            getChildFragmentManager().beginTransaction().remove( f ).commit();
        }
    }

}

I was getting the exception whenever I went to a new fragment and came back. Make sure to remove the fragments in your "cleaup" method, then you can load can reload your views when you come back to the fragment.


If your ViewPager has been on the Fragment, then use not getFragmentManager but getChildFragmentManager.