我有一个列表,其中包含手机中安装的所有应用程序。单击这些部分之一以卸载选定的应用程序。问题是即使应用程序被卸载,仍然在列表中。要删除它,我已经完成了此代码但不起作用(崩溃)
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
int requestCode = 1;
app = appList.get(position);
Uri packageUri = Uri.parse("package:" + app.packagename);
this.position = position;
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageUri);
startActivityForResult(uninstallIntent, requestCode);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
try {
ApplicationInfo packageInfo = getPackageManager().getApplicationInfo(app.packagename, 0);
} catch (PackageManager.NameNotFoundException e) {
appInfoArrayList.remove(position);
adapter.notifyDataSetChanged();
}
}
}
这是日志猫
11-12 17:51:07.188: E/AndroidRuntime(27943): FATAL EXCEPTION: main
11-12 17:51:07.188: E/AndroidRuntime(27943): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.packagename/com.packagename.class}: java.lang.NullPointerException
11-12 17:51:07.188: E/AndroidRuntime(27943): at android.app.ActivityThread.deliverResults(ActivityThread.java:3495)
11-12 17:51:07.188: E/AndroidRuntime(27943): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3538)
11-12 17:51:07.188: E/AndroidRuntime(27943): at android.app.ActivityThread.access$1100(ActivityThread.java:153)
11-12 17:51:07.188: E/AndroidRuntime(27943): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
11-12 17:51:07.188: E/AndroidRuntime(27943): at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 17:51:07.188: E/AndroidRuntime(27943): at android.os.Looper.loop(Looper.java:137)
11-12 17:51:07.188: E/AndroidRuntime(27943): at android.app.ActivityThread.main(ActivityThread.java:5289)
11-12 17:51:07.188: E/AndroidRuntime(27943): at java.lang.reflect.Method.invokeNative(Native Method)
11-12 17:51:07.188: E/AndroidRuntime(27943): at java.lang.reflect.Method.invoke(Method.java:525)
11-12 17:51:07.188: E/AndroidRuntime(27943): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
11-12 17:51:07.188: E/AndroidRuntime(27943): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
11-12 17:51:07.188: E/AndroidRuntime(27943): at dalvik.system.NativeStart.main(Native Method)
11-12 17:51:07.188: E/AndroidRuntime(27943): Caused by: java.lang.NullPointerException
11-12 17:51:07.188: E/AndroidRuntime(27943): at com.packagename.onActivityResult(myclass:java:87)
11-12 17:51:07.188: E/AndroidRuntime(27943): at android.app.Activity.dispatchActivityResult(Activity.java:5322)
11-12 17:51:07.188: E/AndroidRuntime(27943): at android.app.ActivityThread.deliverResults(ActivityThread.java:3491)
11-12 17:51:07.188: E/AndroidRuntime(27943): ... 11 more
我的意图是在卸载应用程序后将其从列表中删除,但为什么会崩溃以及如何解决?