且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android FragmentManager BackStackRecord.run 抛出 NullPointerException

更新时间:2023-11-19 15:15:58

回答我自己的问题:

当您调用 FragmentTransaction.remove(null);FragmentTransaction.commit(); 时(最终)抛出此异常>

而且,就像 Twice Circled 和 ShinyuX 在评论中指出的那样;当调用 show(null)add(null)attach(null)detach(null) 时方法,可能还有 hide(null)

调用commit()后,事务将在FragmentManager中排队.因此,当您显式调用 FragmentManager.executePendingTransactions() 后正在处理操作时,或者当 FragmentManager 队列线程调用它时,它会抛出 NullPointerException.>

就我而言,我在全局对象中维护片段状态.在那里我检查了片段是否显示,然后删除了可见的片段.但是因为我启动了一个新的 FragmentActivity,这些状态在它们不可见时仍然设置为 true.所以这是一个设计错误.

除了修复设计错误之外,解决方案很简单:在删除片段之前检查FragmentManager.findFragmentByTag()是否返回null.

I sometimes get the following exception when working with Fragments:

FATAL EXCEPTION: main
java.lang.NullPointerException
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:591)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)

The exception occurs when run() of BackStackRecord is called through execPendingTransactions(), when it tries to remove a fragment from the manager.

case OP_REMOVE: {
  Fragment f = op.fragment;
  f.mNextAnim = op.exitAnim; <----
  mManager.removeFragment(f, mTransition, mTransitionStyle);
}
break;

I can't seem to figure out what exactly is causing this? I think it has to do with the backstack of fragments not being cleaned up right when removing fragments.

Answering my own question:

This exception is (eventually) thrown when you call FragmentTransaction.remove(null); and FragmentTransaction.commit();

EDIT: And also, like Twice Circled and shinyuX point out in the comment; when calling the show(null) or add(null), attach(null) and detach(null) methods, and probably also hide(null)

After calling commit(), the transaction will be queued in the FragmentManager. As a result, when the operation is being processed after you explicitly call FragmentManager.executePendingTransactions(), or when the FragmentManager queue thread calls it, it throws a NullPointerException.

In my case, I was maintaining fragment states in a global object. There I checked if the fragment was showing or not, and then removed visible fragments. But because I started a new FragmentActivity, these states were still set to true while they were not visible. So this is a design error.

Other than fixing the design error, the solution was simple: check whether FragmentManager.findFragmentByTag() returned null before removing the fragment.