IllegalStateException: is not currently in the FragmentManager

后端 未结 12 2127
予麋鹿
予麋鹿 2021-01-31 08:40

I know it sounds like a duplicate of FragmentStatePagerAdapter IllegalStateException: is not currently in the FragmentManager but his solution isn\'t relevan

相关标签:
12条回答
  • 2021-01-31 09:05

    The FragmentStatePagerAdapter is a horrible piece of code riddled with bugs acknowledge or not by Google and so I use this code to fix this particular crash:

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // Yet another bug in FragmentStatePagerAdapter that destroyItem is called on fragment that hasnt been added. Need to catch
            try {
                super.destroyItem(container, position, object);
            } catch (IllegalStateException ex) {
                ex.printStackTrace();
            }
        }
    
    0 讨论(0)
  • 2021-01-31 09:06

    Use the following

    final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.add(f, key);
    

    instead of

    mFragmentManager.putFragment(state, key, f);
    

    and pass the bundle explicitly..

    For reference,

    http://developer.android.com/reference/android/app/FragmentTransaction.html#add%28android.app.Fragment,%20java.lang.String%29

    0 讨论(0)
  • 2021-01-31 09:06

    You could try using mFragmentManager.add();

    0 讨论(0)
  • 2021-01-31 09:07

    Write in Activity onCreate() method :

    pager = (ViewPager) findViewById(R.id.pager);
        adapter = new SwipePagerAdapter(getSupportFragmentManager());
        pageOneFragment = new PageOneFragment();
        adapter.addFragment(pageOneFragment);
    

    Adapter code:

    public class SwipePagerAdapter extends FragmentStatePagerAdapter
    {
    private final ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
    
    public SwipePagerAdapter(FragmentManager fm)
    {
        super(fm);
    }
    
    @Override
    public Fragment getItem(int position)
    {
        return mFragments.get(position);
    }
    
    @Override
    public int getCount()
    {
        return mFragments.size();
    }
    
    public void addFragment(Fragment fragment)
    {
        mFragments.add(fragment);
        notifyDataSetChanged();
    }
    
    @Override
    public void destroyItem(ViewGroup container, int position, Object object)
    {
        super.destroyItem(container, position, object);
    }
    
    @Override
    public CharSequence getPageTitle(int position)
    {
        return super.getPageTitle(position);
    }}
    
    0 讨论(0)
  • 2021-01-31 09:10

    Double-check whether you've implemented onSaveInstanceState and onRestoreInstanceState in your activity, and verify that they're correctly saving and loading the state of your fragments.

    0 讨论(0)
  • 2021-01-31 09:12

    Try using

    use fragmentTransaction.add()
    
    0 讨论(0)
提交回复
热议问题