当前位置: 首页 > 知识库问答 >
问题:

迁移到AndroidX后,应用程序因试图调用AndroidX而崩溃。碎片应用程序。碎片管理。空引用上的isDestroyed()

郎同化
2023-03-14

整个堆栈仅包括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)

它发生在替换主活动中的片段时:

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

共有3个答案

毋胜涝
2023-03-14

最后一天,我在这个问题上纠结不定,终于找到了解决办法。我用的是gradle

implementation 'androidx.constraintlayout:constraintlayout:1.1.2'

我用这个代码替换了我的片段。

 getActivity().getSupportFragmentManager().beginTransaction().detach(new    KiKorbo()).replace(R.id.calendar1, caldroidFragment).attach(caldroidFragment)
            .addToBackStack(null).commit();

这里是KiKorbo。java是我的父片段,我替换了caldroidFragment。

毋玺
2023-03-14

在我的模块渐变中,我通过还原来修复它

implementation 'androidx.appcompat:appcompat:1.1.0-beta01'

implementation 'androidx.appcompat:appcompat:1.1.0-alpha05'

(上面关于childFragmentManager的回答没有帮助。)

管梓
2023-03-14

我在myFragment中找到了一段代码。导致这种情况的附件:

这是一种解决方法,避免了在使用ViewPager选项卡时出现错误“Java.lang.IllegalStateException活动已被破坏”

        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中,不需要这种变通方法。

 类似资料: