我收到此错误崩溃

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

当 Activity onStop()时,以及当我从FragmentTransaction中删除 fragment 时

我加了
@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();

最佳答案

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

10-08 15:05