gpt4 book ai didi

java - 迁移到 AndroidX 后,应用程序崩溃并尝试在空引用上调用 androidx.fragment.app.FragmentManagerImpl.isDestroyed()

转载 作者:太空宇宙 更新时间:2023-11-03 11:46:21 26 4
gpt4 key购买 nike

完整的堆栈仅包括 android 核心代码:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean androidx.fragment.app.FragmentManagerImpl.isDestroyed()' on a null object reference
at androidx.fragment.app.Fragment.performDetach(Fragment.java:2844)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1033)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1237)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:434)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2075)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1865)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1820)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1726)
at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

在主 Activity 中替换 fragment 时发生:

Runnable mPendingRunnable = new Runnable() {
@Override
public void run() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame, fragment);
fragmentTransaction.commitAllowingStateLoss();
}
};

最佳答案

我在 myFragment.onDetach 中找到一段代码导致了这个:

这是来自 Getting the error "Java.lang.IllegalStateException Activity has been destroyed" when using tabs with ViewPager 的解决方法

        try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}

现在,在 androidx 中,不需要此解决方法。

关于java - 迁移到 AndroidX 后,应用程序崩溃并尝试在空引用上调用 androidx.fragment.app.FragmentManagerImpl.isDestroyed(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56618453/

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