0

我添加了一个选项来为我的 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

我找不到原因,请帮忙。

4

2 回答 2

5

查看您的代码会有所帮助,尤其是onActivityResult包含第 308 行的 ThemeSettingsActivity.java 方法。

但是,AsyncTask 的设计存在一个弱点,这可能是造成这种情况的原因。当您改变方向时,您的活动将被销毁并重新创建。但是,AsyncTask(特别是如果它是 Activity 的内部类)仍在与被破坏的 Activity 交互。Android 文档建议保存任务状态并在销毁活动时销毁任务,然后在重新创建活动时从保存的状态构造新任务。请参阅Google Groups 上的此讨论主题,其中讨论了以允许任务继续的方式解决问题的几个想法。

于 2012-04-19T06:07:41.170 回答
2

正如 Ted 所说,您可能会被糟糕的 AsyncTask 使用所困扰。我写了一篇关于 AsyncTask 陷阱的博客文章。您可能会发现它与您的情况有关。

http://www.shanekirk.com/2012/04/asynctask-missteps/

-谢恩

于 2012-04-19T15:03:28.217 回答