迁移到AndroidX之后,应用程序因尝试在null ref上调用androidx.fragment.app.FragmentManagerImpl.isDestroyed()而崩溃

时间:2019-06-16 11:37:36

标签: java android androidx

完整的堆栈仅包含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();
            }
        };

5 个答案:

答案 0 :(得分:3)

在模块的gradle中,我通过还原对其进行了修复

l

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

(上述关于childFragmentManager的答案没有帮助。)

答案 1 :(得分:2)

我在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中,不需要此解决方法。

答案 2 :(得分:0)

我的基本片段中有这段代码

   try {
            val childFragmentManager = androidx.fragment.app.Fragment::class.java.getDeclaredField(
                    "mChildFragmentManager"
            )
            childFragmentManager.isAccessible = true
            childFragmentManager.set(this, null)
        } catch (e: NoSuchFieldException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        }

删除此代码后,崩溃已修复。

答案 3 :(得分:0)

最近几天我一直在努力解决这个问题,最后我找到了解决方案。 在这里我正在使用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。

答案 4 :(得分:0)

在我的模块的 gradle 中,我通过恢复修复了它

implementation 'androidx.appcompat:appcompat:1.2.5'

implementation 'androidx.appcompat:appcompat:1.1.0'