我正在尝试使用我通过搜索找到的常见方法动态地将片段添加到ViewPager
,我尝试了两种方法:
第一个来自作为ViewPager的父级的Activity:
Light_Center fragment = new Light_Center(); // the Fragment Activity
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(fragment, "num_" + i);
ft.commit(); // this is where i got the Exception
第二个来自片段活动本身(Light_Center.Class):
FragmentManager childFragMan = getChildFragmentManager();
FragmentTransaction childFragTrans = childFragMan.beginTransaction();
Light_Center fragB = new Light_Center();
childFragTrans.add(R.id.light_center_fragment, fragB);
childFragTrans.addToBackStack("B");
childFragTrans.commit(); // this is where i got the Exception
这两种方式在commit()
行给了我相同的例外:
04-29 12:46:04.099: E/AndroidRuntime(1307): FATAL EXCEPTION: main
04-29 12:46:04.099: E/AndroidRuntime(1307): java.lang.IllegalStateException: Activity has been destroyed
04-29 12:46:04.099: E/AndroidRuntime(1307): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1365)
04-29 12:46:04.099: E/AndroidRuntime(1307): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
04-29 12:46:04.099: E/AndroidRuntime(1307): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
04-29 12:46:04.099: E/AndroidRuntime(1307): at com.automation.isolace.Lighting$11.onClick(Lighting.java:339)
04-29 12:46:04.099: E/AndroidRuntime(1307): at android.view.View.performClick(View.java:4084)
04-29 12:46:04.099: E/AndroidRuntime(1307): at android.view.View$PerformClick.run(View.java:16966)
04-29 12:46:04.099: E/AndroidRuntime(1307): at android.os.Handler.handleCallback(Handler.java:615)
04-29 12:46:04.099: E/AndroidRuntime(1307): at android.os.Handler.dispatchMessage(Handler.java:92)
04-29 12:46:04.099: E/AndroidRuntime(1307): at android.os.Looper.loop(Looper.java:137)
04-29 12:46:04.099: E/AndroidRuntime(1307): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-29 12:46:04.099: E/AndroidRuntime(1307): at java.lang.reflect.Method.invokeNative(Native Method)
04-29 12:46:04.099: E/AndroidRuntime(1307): at java.lang.reflect.Method.invoke(Method.java:511)
04-29 12:46:04.099: E/AndroidRuntime(1307): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
我发现很多关于 StackOverFlow 的问题代表了相同的异常,但是我的使用方式都没有。
答案 0 :(得分:0)
正在发生的事情是你开始getChildFragmentManager().beginTransaction()
并且在你能够执行.commit()
之前你的活动或片段被破坏了。我有一个类似的问题。您将需要检查您的状态并确保在提交之前仍然存在问题。发生此错误时,您的应用中还会发生什么?