gpt4 book ai didi

android - 删除 fragment 时崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 11:01:15 25 4
gpt4 key购买 nike

我在平板电脑上崩溃了,这对我来说很奇怪:

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 上。 Debug模式无法帮助我找出位置。当我点击一个删除 fragment 的按钮时,它发生了,但是代码的“fragment 删除”部分得到了正确执行,在我的 OnClickListener 执行之后发生了一些事情。

我删除了交易动画并遇到了同样的崩溃。

以前有人经历过吗?

谢谢

最佳答案

似乎解决方案不是保留 fragment 的引用而是使用标签找到它:

之前:

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();

关于android - 删除 fragment 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42420071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com