FragmentStatePagerAdapter IllegalStateException:< MyFragment>目前不在FragmentManager中 [英] FragmentStatePagerAdapter IllegalStateException: <MyFragment> is not currently in the FragmentManager

查看:110
本文介绍了FragmentStatePagerAdapter IllegalStateException:< MyFragment>目前不在FragmentManager中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,在使用FragmentStatePagerAdapter的活动的onResume()中,我得到了这一点.使用设备的后退按钮时.不总是.不可复制.

I'm getting this on some cases, in onResume(), of an activity which uses a FragmentStatePagerAdapter. When using device's back button. Not always. Not reproducible.

我正在使用最新版本(8)的支持包v4.

I'm using support package v4, last revision (8).

已经用Google搜索过,没有找到有用的答案.

Already searched with google, no success finding a useful answer.

在源代码中查找,它被抛出:FragmentManager.java

Looking in the source, it's thrown here: FragmentManager.java

@Override
public void putFragment(Bundle bundle, String key, Fragment fragment) {
    if (fragment.mIndex < 0) {
        throw new IllegalStateException("Fragment " + fragment
                + " is not currently in the FragmentManager");
    }
    bundle.putInt(key, fragment.mIndex);
}

但是为什么片段的索引是<那里有0?

But why is the index of fragment < 0 there?

实例化片段的代码:

@Override
public Fragment getItem(int position) {
    Fragment fragment = null;

    switch(position) {
        case 0:
            fragment = MyFragment.newInstance(param1);
            break;
        case 1:
            fragment = MyFragment2.newInstance(param2, param3);
            break;
    }
    return fragment;
}

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

推荐答案

知道了,原因是,我每次在onResume()中都要实例化适配器.

Got it, the reason was, that I'm intantiating the Adapter each time in onResume().

如果我仅在活动的生命周期中实例化适配器一次,则不会再发生这种情况.

If I instantiate the adapter only once, in the life cycle of the activity, this does not happen anymore.

这篇关于FragmentStatePagerAdapter IllegalStateException:&lt; MyFragment&gt;目前不在FragmentManager中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆