简体   繁体   中英

Uninstall application programmatically: crash rotating the device

I'm trying to make a listview with all the applications installed inside where you can make multiple uninstalls applications. I have a problem. This is the code.

When I display the dialog uninstall the selected application if I rotate the device the application crashes with this error.

01-19 18:50:13.571: E/AndroidRuntime(25609): FATAL EXCEPTION: main
01-19 18:50:13.571: E/AndroidRuntime(25609): Process: com.myPackage.name, PID: 25609
01-19 18:50:13.571: E/AndroidRuntime(25609): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.myPackage.name/com.myPackage.name.class}: java.lang.NullPointerException
01-19 18:50:13.571: E/AndroidRuntime(25609):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3385)
01-19 18:50:13.571: E/AndroidRuntime(25609):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3428)
01-19 18:50:13.571: E/AndroidRuntime(25609):    at android.app.ActivityThread.access$1300(ActivityThread.java:145)
01-19 18:50:13.571: E/AndroidRuntime(25609):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1254)
01-19 18:50:13.571: E/AndroidRuntime(25609):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-19 18:50:13.571: E/AndroidRuntime(25609):    at android.os.Looper.loop(Looper.java:136)
01-19 18:50:13.571: E/AndroidRuntime(25609):    at android.app.ActivityThread.main(ActivityThread.java:5081)
01-19 18:50:13.571: E/AndroidRuntime(25609):    at java.lang.reflect.Method.invokeNative(Native Method)
01-19 18:50:13.571: E/AndroidRuntime(25609):    at java.lang.reflect.Method.invoke(Method.java:515)
01-19 18:50:13.571: E/AndroidRuntime(25609):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
01-19 18:50:13.571: E/AndroidRuntime(25609):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-19 18:50:13.571: E/AndroidRuntime(25609):    at dalvik.system.NativeStart.main(Native Method)
01-19 18:50:13.571: E/AndroidRuntime(25609): Caused by: java.lang.NullPointerException
01-19 18:50:13.571: E/AndroidRuntime(25609):    at com.myPackage.name.class.onActivityResult(myclass.java:91)
01-19 18:50:13.571: E/AndroidRuntime(25609):    at android.app.Activity.dispatchActivityResult(Activity.java:5423)
01-19 18:50:13.571: E/AndroidRuntime(25609):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3381)
01-19 18:50:13.571: E/AndroidRuntime(25609):    ... 11 more

The line 91 is for(int a=0; a

Try putting this in your AndroidManifest.xml file within the definition of your Activity :

android:configChanges="orientation|keyboardHidden|screenSize"

This will prevent your app from restart if the device is rotated.

Some device configurations can change during runtime (such as screen orientation, keyboard availability, and language). When such a change occurs, Android restarts the runningActivity(onDestroy() is called, followed by onCreate())....

To properly handle a restart, it is important that your activity restores its previous state through the normal Activity lifecycle, in which Android calls onSaveInstanceState() before it destroys your activity so that you can save data about the application state. You can then restore the state duringonCreate()or onRestoreInstanceState().

If your application doesn't need to update resources during a specific configuration changeandyou have a performance limitation that requires you to avoid the activity restart, then you can declare that your activity handles the configuration change itself, which prevents the system from restarting your activity.

And you can use android:configChanges="WITH NEEDED ATTRIBUTES PUT HERE"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM