嵌套片段崩溃

时间:2015-04-24 14:00:51

标签: android android-fragments

我正在使用嵌套片段并使用childfragmentmanager来处理内部片段。当操作系统重新创建片段时,我得到以下错误(我通过在应用程序背景下强行销毁活动来测试它)

java.lang.IllegalStateException: Fragment no longer exists for key     android:target_state: index 4

当我将以下行添加到片段时,崩溃不会发生

@Override
public void onSaveInstanceState(Bundle outState){
    setTargetFragment(null, -1);
    super.onSaveInstanceState(outState);
}

我不确定是什么原因造成的。有没有人遇到过这个问题。以下是堆栈跟踪

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.debug/com.ui.MainActivity}: java.lang.IllegalStateException: Fragment no longer exists for key android:target_state: index 2
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.IllegalStateException: Fragment no longer exists for key android:target_state: index 2
        at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:584)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:883)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
        at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1912)
        at android.support.v4.app.Fragment.performCreate(Fragment.java:1776)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
        at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1912)
        at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:266)
        at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
        at com.ui.MainActivity.onCreate(MainActivity.java:68)
        at android.app.Activity.performCreate(Activity.java:5933)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

1 个答案:

答案 0 :(得分:1)

尝试在嵌套片段中使用getParentFragment(),而不是使用setTargetFragment()getTargetFragment()