在 做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: main11-15 09:04:21.023: E/AndroidRuntime(4057): java.lang.NullPointerException11-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.

更多相关文章

  1. Android Content Provider 之Contacts数据库操作
  2. android通过Instrumentation来模拟键盘点击操作
  3. SQlite数据库(2)----android 操作
  4. Android使用GreenDAO数据库的基本操作及数据库升级迁移
  5. android Menu菜单操作(偏门--监听 more 操作)
  6. Android设计元素-操作栏
  7. ANDROID JNI之JAVA域与c域的互操作
  8. java for android sqlite数据的操作:说实话,我用了ADO.NET的思想,自
  9. Android同步方法中异步操作获取返回值。

随机推荐

  1. 谈谈android中的MVC,MVP和MVVM MVC
  2. Android(安卓)View绘制及实践
  3. Android实现简易版弹钢琴效果
  4. Android Studio(4)---开发人员工作流程基
  5. android动态加载(二)
  6. Android(安卓)Mms 接收信息流程
  7. Android加载动态库不成功处理方法
  8. android评分条RatingBar自定义设置
  9. Android五大布局介绍&属性设置大全
  10. Android 开发艺术探索读书笔记 10 -- And