【问题标题】:'boolean androidx.fragment.app.FragmentManagerImpl.isDestroyed()' on a null object reference'boolean androidx.fragment.app.FragmentManagerImpl.isDestroyed()' 在空对象引用上
【发布时间】:2019-07-07 20:14:28
【问题描述】:

我收到此错误崩溃

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();

【问题讨论】:

  • 如果您在 Activity 中调用具有反射的方法,当您从非自己调用的框架生命周期方法中获得空指针时,您不应该感到惊讶。
  • 我需要做什么?

标签: android fragment androidx


【解决方案1】:

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

【讨论】:

  • 听起来你还没有从任何地方删除它。变量本身是非空的,因此它为空的唯一方法是通过反射将其设置为空。
  • @ianhanniballake 你救了我的命,伙计 +1
  • 当我使用 Caldroid Fragment 并且这个 Fragment 被破坏时,只会出现这个问题。我的应用程序与 Androidx 兼容,但 CaldroidFragment 库是 nou。任何帮助将不胜感激
  • @YashBhardwaj 比 cmets...
  • 从 BaseFragment 及其作品中移除 onDetach() 谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-26
  • 2017-07-16
  • 1970-01-01
  • 2015-08-21
  • 2021-05-04
  • 2016-06-12
相关资源
最近更新 更多