IllegalStateException - 片段不再存在

时间:2017-11-28 21:02:10

标签: android android-fragments illegalstateexception

我在Google Play控制台中看到IllegalStateException,我无法重现。

这是堆栈跟踪:

java.lang.RuntimeException: 
      at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3253)
      at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3349)
      at android.app.ActivityThread.access$1100 (ActivityThread.java:221)
      at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1794)
      at android.os.Handler.dispatchMessage (Handler.java:102)
      at android.os.Looper.loop (Looper.java:158)
      at android.app.ActivityThread.main (ActivityThread.java:7224)
      at java.lang.reflect.Method.invoke (Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1230)
      at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1120)
Caused by: java.lang.IllegalStateException: 
      at android.support.v4.app.FragmentManagerImpl.getFragment (FragmentManager.java:668)
      at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:999)
      at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1252)
      at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1234)
      at android.support.v4.app.FragmentManagerImpl.dispatchCreate (FragmentManager.java:2041)
      at android.support.v4.app.Fragment.performCreate (Fragment.java:1964)
      at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1029)
      at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1252)
      at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1234)
      at android.support.v4.app.FragmentManagerImpl.dispatchCreate (FragmentManager.java:2041)
      at android.support.v4.app.Fragment.performCreate (Fragment.java:1964)
      at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1029)
      at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1252)
      at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1234)
      at android.support.v4.app.FragmentManagerImpl.dispatchCreate (FragmentManager.java:2041)
      at android.support.v4.app.FragmentController.dispatchCreate (FragmentController.java:163)
      at android.support.v4.app.FragmentActivity.onCreate (FragmentActivity.java:331)
      at android.support.v7.app.AppCompatActivity.onCreate (AppCompatActivity.java:85)
      at com.example.eliran.forum.RootActivity.onCreate (RootActivity.java:53)
      at com.example.eliran.forum.MainActivity.onCreate (MainActivity.java:58)
      at android.app.Activity.performCreate (Activity.java:6876)
      at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1135)
      at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3206)

我有使用NavigationView的活动,其中包含片段。

我怀疑当用户按下后退按钮并且调用包含以下代码的函数时会发生此异常:

if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
    getSupportFragmentManager().popBackStack();
}

Google-Play控制台无法显示此信息,但这是例外错误:

    throwException(new IllegalStateException("Fragment no longer exists for key "
            + key + ": index " + index));

1 个答案:

答案 0 :(得分:1)

您可能在代码中的某处使用setTargetFragment()。 只是避免使用它。 相反,通过回调和监听器在fragemnts之间进行通信,如下所示: 在fragmentDialog中 -

protected OnRemovedTopicListener callback = null;

public interface OnRemovedTopicListener {
    public abstract void onTopicRemoved(int position);
}

public void setOnRemovedTopicListener(OnRemovedTopicListener l){
    callback = l;
}

并使用回调 - callback.onTopicRemoved(位置);

在父片段中 -

    dialogFragment.setOnRemovedTopicListener(new ForumTopicDialogFragment.OnRemovedTopicListener() {
        @Override
        public void onTopicRemoved(int position) {
            // you code here
        }
    });