【发布时间】: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 中调用具有反射的方法,当您从非自己调用的框架生命周期方法中获得空指针时,您不应该感到惊讶。
-
我需要做什么?