空对象引用上的'boolean androidx.fragment.app.FragmentManagerImpl.isDestroyed()'

时间:2019-07-07 20:14:28

标签: android fragment androidx

我收到此错误崩溃

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean androidx.fragment.app.FragmentManagerImpl.isDestroyed()' on a null object reference
     `
`

活动onStop()以及从FragmentTransaction中删除片段时

我添加了

`@Override
public void onDetach() {
    super.onDetach();

    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);
    }
}`

我所有的片段,但这没有帮助

我的代码

FragmentTransaction t = getSupportFragmentManager().beginTransaction();
                    //t.replace(R.id.calendar1, weekendCaldroidFragment);
                    t.detach(weekendCaldroidFragment).add(R.id.calendar1, weekendCaldroidFragment).attach(weekendCaldroidFragment).commitNowAllowingStateLoss();

1 个答案:

答案 0 :(得分:0)

onDetach()中删除所有代码-在任何最新版本的Fragments上都不需要,而正是导致崩溃的原因。