为什么在空对象引用上抛出androidx.collection.SparseArrayCompat.size()

时间:2019-07-18 08:55:43

标签: android android-activity nullpointerexception fragment android-lifecycle

在我的应用程序中,当我尝试将应用程序移至后台时会抛出此异常。

java.lang.NullPointerException: Attempt to invoke virtual method 'int androidx.collection.SparseArrayCompat.size()' on a null object reference
    at androidx.fragment.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:500)
    at androidx.appcompat.app.AppCompatActivity.onSaveInstanceState(AppCompatActivity.java:510)
    at android.app.Activity.performSaveInstanceState(Activity.java:1503)
    at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1308)
    at android.app.ActivityThread.callCallActivityOnSaveInstanceState(ActivityThread.java:4943)
    at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:4231)
    at android.app.ActivityThread.handleStopActivity(ActivityThread.java:4290)
    at android.app.ActivityThread.-wrap28(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1665)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6682)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

有人可以解释为什么抛出此异常吗?

1 个答案:

答案 0 :(得分:0)

FragmentActivity中扩展的AppCompatActivity中,只要调用onSaveInstanceState mPendingFragmentActivityResults就为空。该字段mPendingFragmentActivityResults用于保留调用Frament.startActivityForResult()的位置的地图,以便它可以使用onActivityResult将结果分派回适当的片段。可能导致此情况的一件事可能是您在“开发人员选项”中设置了“不保留活动”设置。无论如何调试这些类并尝试查找原因。