0

I have added an option to apply different theme for my android phone. while applying i am starting an async task. If i change the orientation while applying it i am getting an Error which is given below.

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

I am not able to find the reason of this please help.

4

2 に答える 2

5

It would help to see your code, particularly the onActivityResult method of ThemeSettingsActivity.java that includes line 308.

However, there's a weakness in the design of AsyncTask that might account for this. When you change orientation, your activity is destroyed and recreated. However, the AsyncTask (especially if it is an inner class of the activity) is still interacting with the destroyed activity. The Android docs suggest saving the task state and destroying the task when the activity is being destroyed, and then constructing a new task from the saved state when the activity is recreated. See this discussion thread on Google Groups, where several ideas are discussed for working around the problem in a way that allow the task to continue.

于 2012-04-19T06:07:41.170 に答える
2

As Ted said, you're probably getting bitten by poor AsyncTask usage. I wrote a blog entry on AsyncTask pitfalls. You might find it relevant to your situation.

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

-Shane

于 2012-04-19T15:03:28.217 に答える