android throw InvocationTargetException,How to modify the error
ActivityManager am = (ActivityManager)this.getSystemService(this.ACTIVITY_SERVICE);
try {
clearMethod = am.getClass()
.getMethod("clearApplicationUserData", String.class,
开发者_如何学运维IPackageDataObserver.class);
} catch (Exception e) {
Log.e("Error", "Android Error",e);
clearMethod = null;
}
if(clearMethod!=null){
try {
clearMethod.invoke(am,"com.android.browser",new ClearUserDataObserver());
} catch (Exception e) {
Log.e("Error", "Android Exception",e);
}
}
Exception
05-26 08:34:13.056: ERROR/Error(739): java.lang.reflect.InvocationTargetException
05-26 08:34:13.056: ERROR/Error(739): at android.app.ActivityManager.clearApplicationUserData(ActivityManager.java:475)
05-26 08:34:13.056: ERROR/Error(739): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 08:34:13.056: ERROR/Error(739): at java.lang.reflect.Method.invoke(Method.java:521)
05-26 08:34:13.056: ERROR/Error(739): at com.iwidsets.clear.manager.AndClear.onCreate(AndClear.java:34)
05-26 08:34:13.056: ERROR/Error(739): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
05-26 08:34:13.056: ERROR/Error(739): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
05-26 08:34:13.056: ERROR/Error(739): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
05-26 08:34:13.056: ERROR/Error(739): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
05-26 08:34:13.056: ERROR/Error(739): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
05-26 08:34:13.056: ERROR/Error(739): at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 08:34:13.056: ERROR/Error(739): at android.os.Looper.loop(Looper.java:123)
05-26 08:34:13.056: ERROR/Error(739): at android.app.ActivityThread.main(ActivityThread.java:4203)
05-26 08:34:13.056: ERROR/Error(739): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 08:34:13.056: ERROR/Error(739): at java.lang.reflect.Method.invoke(Method.java:521)
05-26 08:34:13.056: ERROR/Error(739): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
05-26 08:34:13.056: ERROR/Error(739): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
05-26 08:34:13.056: ERROR/Error(739): at dalvik.system.NativeStart.main(Native Method)
05-26 08:34:13.056: ERROR/Error(739): Caused by: java.lang.SecurityException: 739 does not have permission:android.permission.CLEAR_APP_USER_DATA to clear datafor process:com.android.browser
05-26 08:34:13.056: ERROR/Error(739): at android.os.Parcel.readException(Parcel.java:1218)
05-26 08:34:13.056: ERROR/Error(739): at android.os.Parcel.readException(Parcel.java:1206)
05-26 08:34:13.056: ERROR/Error(739): at android.app.ActivityManagerProxy.clearApplicationUserData(ActivityManagerNative.java:2016)
05-26 08:34:13.056: ERROR/Error(739): ... 17 more
This is my androidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.iwidsets.clear.manager"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndClear"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.CLEAR_APP_USER_DATA"></uses-permission>
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"></uses-permission>
</manifest>
You can't modify the error.
You are missing a permission, check out this line in your logcat output:
.SecurityException: 739 does not have permission:android.permission.CLEAR_APP_USER_DATA to clear datafor process:com.android.browser
As the java API states:
InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor.
It seems that somewhere in your method clearApplicationUserData there is an Exception
Edit: an as pentium10 pointed out there is the permission missing for your clearApplicationUserData method.
public boolean clearApplicationUserData(String packageName, IPackageDataObserver observer) {
try {
return ActivityManagerNative.getDefault().clearApplicationUserData(packageName,
observer);
} catch (RemoteException e) {
return false;
}
}
精彩评论