简体   繁体   English

以编程方式卸载应用程序:旋转设备时崩溃

[英]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 第91行是for(int a = 0; a

Try putting this in your AndroidManifest.xml file within the definition of your Activity : 尝试将其放在Activity定义内的AndroidManifest.xml文件中:

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()).... 发生此类更改时,Android将重新启动runningActivity(onDestroy(),然后调用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. 为了正确处理重启,重要的是您的活动必须在正常的活动生命周期内恢复其先前状态,在Android生命周期中,Android会在销毁活动之前先调用onSaveInstanceState(),以便您可以保存有关应用程序状态的数据。 You can then restore the state duringonCreate()or onRestoreInstanceState(). 然后,您可以在onCreate()或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" 而且您可以使用android:configChanges =“在此处放置需要的属性”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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