java.lang.IllegalStateException:Fragement不再存在键F1:指标3指标、存在、lang、java

2023-09-04 11:14:09 作者:原来都是梦

我想,以实现适当的修正明白这一点例外。

I want to understand this exception in order to implement a proper fix.

概述:

有一个ViewPager,它采用了FragmentStatePagerAdapter通过的getItem实例2片段和 MyFragmentClass.newInstance(...)

There's a ViewPager and it uses a FragmentStatePagerAdapter to instantiate 2 fragments via getItem and MyFragmentClass.newInstance(...).

适配器的的getItem看起来是这样的:

Adapter's getItem looks like this:

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

    switch(position) {
        case 0:
            fragment = MyFragment2.newInstance(par1);
            break;
        case 1:
            fragment = MyFragment2.newInstance(par2, par3);
            break;
    }
    return fragment;
}

问题:

当活动被破坏,再次创建,适配器再次intantiated,与 MyFragmentClass.newInstance(...) ...但随后再次创造了碎片在这条线:

When the activity is destroyed, and created again, the adapter is intantiated again, the fragments created again with MyFragmentClass.newInstance(...)... but then on this line:

pager.setAdapter(适配器);

我得到的提到的异常。

我看了看那里的抛出异常的来源,那就是:

I looked in the source where the exception is thrown, it's this:

@Override
public Fragment getFragment(Bundle bundle, String key) {
    int index = bundle.getInt(key, -1);
    if (index == -1) {
        return null;
    }
    if (index >= mActive.size()) {
        throw new IllegalStateException("Fragement no longer exists for key "
                + key + ": index " + index);
    }
    Fragment f = mActive.get(index);
    if (f == null) {
        throw new IllegalStateException("Fragement no longer exists for key "
                + key + ": index " + index);
    }
    return f;
}

所以,一个包被传递那里,与一些国家它引用我的老片段,但是这并不符合当前的状态( mActive ),和抗辩被抛出。

So, a bundle is passed there, with some state which references my old fragments, but this doesn't correspond to the current state (mActive), and the exceptio is thrown.

我不明白什么是这背后的想法,或者是办法,我实际上应该实例化碎片...所以我不知道如何解决。

I don't understand what's the idea behind this, or which way I'm actually supposed to instantiate the fragments... so I have no idea how to solve.

我也尝试了一些诀窍,我从其他一些方面得到了:

I also tried some trick I got from some other context:

pager.setOffscreenPageLimit(1);

为了避免当他们关闭屏幕上的片段被破坏(在2页的情况下viewpager,虽然不知道它是否能够很好地处理状态适配器)。但是,不要似乎是相关的,至少,它不能帮助,仍然得到同样的异常。

In order to avoid that the fragments are destroyed when they are off screen (in the case of 2 pages viewpager, although don't know if it works well with state adapter). But don't seems to be related, at least, it doesn't help, still get the same exception.

我所做的,现在是把周围的行try catch块,和好,然后我得到的空白页,而不是崩溃中,也并不好。

What I did right now is put a try catch block around the line, and well, then I get blank pages instead of a crash box, also not good.

推荐答案

如果您不希望片段得到他们的屏幕之外的时候,你应该使用再生 FragmentPagerAdapter 而不是 FragmentStatePagerAdapter

If you don't want the fragments to get reclaimed when they are offscreen, you should be using FragmentPagerAdapter and not FragmentStatePagerAdapter.