我的平板电脑崩溃对我来说非常神秘:
java.lang.NullPointerException: Attempt to read from field 'int android.support.v4.app.Fragment.mContainerId' on a null object reference
at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1012)
at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:971)
at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:95)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2143)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
它只发生在我的api 24 Pixel C上。调试模式无法帮助我找到答案。它发生在我用一个删除片段的按钮时,但是"片段删除"部分代码被正确执行,执行我的OnClickListener后发生了一些事情。
我删除了交易动画并遇到了同样的崩溃。
以前有人经历过吗?
由于
答案 0 :(得分:0)
似乎解决方案不是保留片段的引用,而是使用标记找到它:
之前:
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
courseLauncherFragment = CourseLauncherFragment.newInstance(courseId);
fragmentTransaction.replace(R.id.launcher_finish_frame_layout, courseLauncherFragment);
fragmentTransaction.commit();
getSupportFragmentManager().executePendingTransactions();
然后删除它:
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left);
fragmentTransaction.remove(courseLauncherFragment);
fragmentTransaction.commit();
现在:
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.launcher_finish_frame_layout, CourseLauncherFragment.newInstance(courseId), LAUNCHER_FRAGMENT_TAG);
fragmentTransaction.commit();
getSupportFragmentManager().executePendingTransactions();
然后:
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left);
Fragment fragment = fragmentManager.findFragmentByTag(LAUNCHER_FRAGMENT_TAG);
fragmentTransaction.remove(fragment);
fragmentTransaction.commit();
答案 1 :(得分:0)
在通过交易移除之前,应检查片段null
。尝试删除null
的片段时遇到了同样的问题。您可以查看详细信息here