Android:自定义View保存状态导致Activity onCreate中的NPE
我有一个在我的应用程序的片段中使用的自定义视图,如果您远离应用程序执行多任务并让它停留很长时间,然后重新打开应用程序,它似乎会导致空指针异常。这确实很难重现,但这是堆栈跟踪:
10-03 13:07:19.560: 错误/AndroidRuntime(7723): 致命异常: main 10-03 13:07:19.560: 错误/AndroidRuntime(7723): java.lang.RuntimeException:无法启动活动 组件信息{com.mycompany.tablet/com.mycompany.activities.MainActivity}: java.lang.NullPointerException:预期接收者类型 com.mycompany.views.CustomView$SavedState,不为空 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1751) 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767) 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 android.app.ActivityThread.access$1500(ActivityThread.java:122) 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005) 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 android.os.Handler.dispatchMessage(Handler.java:99) 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 android.os.Looper.loop(Looper.java:132) 10-03 13:07:19.560: 错误/AndroidRuntime(7723):位于 android.app.ActivityThread.main(ActivityThread.java:4028) 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 java.lang.reflect.Method.invokeNative(原生方法) 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 java.lang.reflect.Method.invoke(Method.java:491) 10-03 13:07:19.560: 错误/AndroidRuntime(7723):位于 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844) 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 dalvik.system.NativeStart.main(本机方法)10-03 13:07:19.560: 错误/AndroidRuntime(7723):由以下原因引起:java.lang.NullPointerException: 预期接收者类型 com.mycompany.views.CustomView$SavedState,不为空 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 java.lang.reflect.Field.getField(本机方法) 10-03 13:07:19.560: 错误/AndroidRuntime(7723):位于 java.lang.reflect.Field.get(Field.java:235) 10-03 13:07:19.560: 错误/AndroidRuntime(7723):位于 android.os.Parcel.readParcelable(Parcel.java:1947) 10-03 13:07:19.560: 错误/AndroidRuntime(7723):位于 android.os.Parcel.readValue(Parcel.java:1846) 10-03 13:07:19.560: 错误/AndroidRuntime(7723):位于 android.os.Parcel.readSparseArrayInternal(Parcel.java:2112) 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 android.os.Parcel.readSparseArray(Parcel.java:1568) 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 android.os.Parcel.readValue(Parcel.java:1903) 10-03 13:07:19.560: 错误/AndroidRuntime(7723):位于 android.os.Parcel.readMapInternal(Parcel.java:2083) 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 android.os.Bundle.unparcel(Bundle.java:215) 10-03 13:07:19.560: 错误/AndroidRuntime(7723):位于 android.os.Bundle.getSparseParcelableArray(Bundle.java:1217) 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 android.app.FragmentManagerImpl.moveToState(FragmentManager.java:681) 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 android.app.FragmentManagerImpl.moveToState(FragmentManager.java:926) 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 android.app.FragmentManagerImpl.moveToState(FragmentManager.java:909) 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 android.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1574) 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 android.app.Activity.onCreate(Activity.java:869) 10-03 13:07:19.560: 错误/AndroidRuntime(7723):位于 com.webtrends.mobile.android.WebtrendsActivity.onCreate(未知 来源)10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 com.mycompany.activities.MainActivity.onCreate(MainActivity.java:173) 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 10-03 13:07:19.560: 错误/AndroidRuntime(7723): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715) 10-03 13:07:19.560: 错误/AndroidRuntime(7723): ... 11 更多 10-03 13:07:19.560:警告/ActivityManager(286):强制完成活动 com.mycompany.tablet/com.mycompany.activities.MainActivity 10-03 13:07:19.600:调试/dalvikvm(7723):GC_CONCURRENT 释放了 238K,5% 可用 6461K/6791K,暂停 3ms+3ms 10-03 13:07:19.650: INFO/SurfaceFlinger(223):id=1946 删除RecentsPanel idx=5 地图 大小=4 10-03 13:07:19.650:错误/android.os.Debug(286):转储状态> /数据/日志/dumpstate_app_error
I have a custom view that's being used inside a Fragment in my app that seems to be causing a null pointer exception if you multitask away from the app and let it sit a long time, then open the app back up. It's really hard to reproduce, but here's the stack trace:
10-03 13:07:19.560: ERROR/AndroidRuntime(7723): FATAL EXCEPTION: main
10-03 13:07:19.560: ERROR/AndroidRuntime(7723):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.mycompany.tablet/com.mycompany.activities.MainActivity}:
java.lang.NullPointerException: expected receiver of type
com.mycompany.views.CustomView$SavedState, not null 10-03
13:07:19.560: ERROR/AndroidRuntime(7723): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1751)
10-03 13:07:19.560: ERROR/AndroidRuntime(7723): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
10-03 13:07:19.560: ERROR/AndroidRuntime(7723): at
android.app.ActivityThread.access$1500(ActivityThread.java:122) 10-03
13:07:19.560: ERROR/AndroidRuntime(7723): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
10-03 13:07:19.560: ERROR/AndroidRuntime(7723): at
android.os.Handler.dispatchMessage(Handler.java:99) 10-03
13:07:19.560: ERROR/AndroidRuntime(7723): at
android.os.Looper.loop(Looper.java:132) 10-03 13:07:19.560:
ERROR/AndroidRuntime(7723): at
android.app.ActivityThread.main(ActivityThread.java:4028) 10-03
13:07:19.560: ERROR/AndroidRuntime(7723): at
java.lang.reflect.Method.invokeNative(Native Method) 10-03
13:07:19.560: ERROR/AndroidRuntime(7723): at
java.lang.reflect.Method.invoke(Method.java:491) 10-03 13:07:19.560:
ERROR/AndroidRuntime(7723): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
10-03 13:07:19.560: ERROR/AndroidRuntime(7723): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 10-03
13:07:19.560: ERROR/AndroidRuntime(7723): at
dalvik.system.NativeStart.main(Native Method) 10-03 13:07:19.560:
ERROR/AndroidRuntime(7723): Caused by: java.lang.NullPointerException:
expected receiver of type
com.mycompany.views.CustomView$SavedState, not null 10-03
13:07:19.560: ERROR/AndroidRuntime(7723): at
java.lang.reflect.Field.getField(Native Method) 10-03 13:07:19.560:
ERROR/AndroidRuntime(7723): at
java.lang.reflect.Field.get(Field.java:235) 10-03 13:07:19.560:
ERROR/AndroidRuntime(7723): at
android.os.Parcel.readParcelable(Parcel.java:1947) 10-03 13:07:19.560:
ERROR/AndroidRuntime(7723): at
android.os.Parcel.readValue(Parcel.java:1846) 10-03 13:07:19.560:
ERROR/AndroidRuntime(7723): at
android.os.Parcel.readSparseArrayInternal(Parcel.java:2112) 10-03
13:07:19.560: ERROR/AndroidRuntime(7723): at
android.os.Parcel.readSparseArray(Parcel.java:1568) 10-03
13:07:19.560: ERROR/AndroidRuntime(7723): at
android.os.Parcel.readValue(Parcel.java:1903) 10-03 13:07:19.560:
ERROR/AndroidRuntime(7723): at
android.os.Parcel.readMapInternal(Parcel.java:2083) 10-03
13:07:19.560: ERROR/AndroidRuntime(7723): at
android.os.Bundle.unparcel(Bundle.java:215) 10-03 13:07:19.560:
ERROR/AndroidRuntime(7723): at
android.os.Bundle.getSparseParcelableArray(Bundle.java:1217) 10-03
13:07:19.560: ERROR/AndroidRuntime(7723): at
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:681)
10-03 13:07:19.560: ERROR/AndroidRuntime(7723): at
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:926)
10-03 13:07:19.560: ERROR/AndroidRuntime(7723): at
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:909)
10-03 13:07:19.560: ERROR/AndroidRuntime(7723): at
android.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1574)
10-03 13:07:19.560: ERROR/AndroidRuntime(7723): at
android.app.Activity.onCreate(Activity.java:869) 10-03 13:07:19.560:
ERROR/AndroidRuntime(7723): at
com.webtrends.mobile.android.WebtrendsActivity.onCreate(Unknown
Source) 10-03 13:07:19.560: ERROR/AndroidRuntime(7723): at
com.mycompany.activities.MainActivity.onCreate(MainActivity.java:173)
10-03 13:07:19.560: ERROR/AndroidRuntime(7723): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
10-03 13:07:19.560: ERROR/AndroidRuntime(7723): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
10-03 13:07:19.560: ERROR/AndroidRuntime(7723): ... 11 more 10-03
13:07:19.560: WARN/ActivityManager(286): Force finishing activity
com.mycompany.tablet/com.mycompany.activities.MainActivity 10-03
13:07:19.600: DEBUG/dalvikvm(7723): GC_CONCURRENT freed 238K, 5% free
6461K/6791K, paused 3ms+3ms 10-03 13:07:19.650:
INFO/SurfaceFlinger(223): id=1946 Removed RecentsPanel idx=5 Map
Size=4 10-03 13:07:19.650: ERROR/android.os.Debug(286): Dumpstate >
/data/log/dumpstate_app_error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论