我添加了一个选项,为我的Android手机应用不同的主题。申请时,我正在启动异步任务。如果我在应用它时改变方向,我会得到一个错误,如下所示。
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): FATAL EXCEPTION: main
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): java.lang.RuntimeException: Unable to resume activity {com.android.settings/com.android.settings.ThemeSettingsActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.android.settings/com.android.settings.ThemeSettingsActivity}: java.lang.NullPointerException
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2471)
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2499)
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1994)
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3375)
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): at android.app.ActivityThread.access$700(ActivityThread.java:125)
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1153)
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): at android.os.Looper.loop(Looper.java:137)
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): at android.app.ActivityThread.main(ActivityThread.java:4368)
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): at java.lang.reflect.Method.invokeNative(Native Method)
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): at java.lang.reflect.Method.invoke(Method.java:511)
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): at dalvik.system.NativeStart.main(Native Method)
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.android.settings/com.android.settings.ThemeSettingsActivity}: java.lang.NullPointerException
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): at android.app.ActivityThread.deliverResults(ActivityThread.java:3004)
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2458)
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): ... 13 more
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): Caused by: java.lang.NullPointerException
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): at com.android.settings.ThemeSettingsActivity.onActivityResult(ThemeSettingsActivity.java:308)
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): at android.app.Activity.dispatchActivityResult(Activity.java:4649)
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): at android.app.ActivityThread.deliverResults(ActivityThread.java:3000)
01-01 00:19:25.140: ERROR/AndroidRuntime(3553): ... 14 more
我无法找到这个原因请求帮助。
答案 0 :(得分:5)
查看代码会很有帮助,特别是包含第308行的ThemeSettingsActivity.java的onActivityResult
方法。
然而,AsyncTask的设计存在一个弱点,可能会导致这种情况。更改方向时,您的活动将被销毁并重新创建。但是,AsyncTask(特别是如果它是活动的内部类)仍然与被破坏的活动进行交互。 Android文档建议在活动被销毁时保存任务状态并销毁任务,然后在重新创建活动时从已保存状态构建新任务。请参阅Google网上论坛上的this discussion thread,其中讨论了以允许任务继续的方式解决问题的几个想法。
答案 1 :(得分:2)
正如Ted所说,你可能会被糟糕的AsyncTask使用所困扰。我写了一篇关于AsyncTask陷阱的博客文章。您可能会发现它与您的情况有关。
http://www.shanekirk.com/2012/04/asynctask-missteps/
-Shane