IllegalStateException: Can't change tag of fragment was android:switcher now android:switcher

∥☆過路亽.° 提交于 2019-12-03 10:16:34
Charuක

The FragmentPagerAdapter already caches the Fragments for you. Each fragment is assigned a tag, and then the FragmentPagerAdapter tries to call findFragmentByTag. It only calls getItem() if the result from findFragmentByTag is null.

You're probably getting this error because you're adding the same fragment instance to the list. You should create a new instance for each page.

Example form document :

//...
      public static class MyAdapter extends FragmentPagerAdapter {
            public MyAdapter(FragmentManager fm) {
                super(fm);
            }

            @Override
            public int getCount() {
                return NUM_ITEMS;
            }

            @Override
            public Fragment getItem(int position) {
                return ArrayListFragment.newInstance(position);// IMPORTANT
            }
        }
    //..

Refer :FragmentPagerAdapter

Main thread : Retrieve a Fragment from a ViewPager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!