Android FragmentManager BackStackRecord.run throwing NullPointerException

本文解决了在Android应用中操作Fragments时出现的空指针异常问题,详细介绍了错误发生的原因及如何避免此问题的发生。

在 做hide () show 等操作的时候判空一下就解决这个问题了.


EDIT2: Answered my own question below

I sometimes get the following exception when working with Fragments:

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

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.

EDIT1: The exception occurs when run() of BackStackRecord is called thru 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;

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 then fixing the design error, the solution was simple: check if FragmentManager.findFragmentByTag() returned null before removing the fragment.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值