调用方法时的NPE



为了删除应用程序数据,我试图使用反射,但在调用方法时获得NPE

Class<?> myClass = Class.forName("android.content.pm.IPackageManager");
Method method = myClass.getMethod("clearApplicationUserData", String.class,IPackageDataObserver.class);
method.setAccessible(true);
Log.v("info",method.getName());
Object c = myClass .newInstance();
method.invoke(c,"com.example.android.apis",null);  //NPE

CCD_ 1不返回任何值并且取两个参数CCD_ 2和CCD_。

我是否传递了正确的参数?

或者我该如何解决问题?

调试时的StackTrace。

 01-31 17:50:07.125: V/info(969): clearApplicationUserData
 01-31 17:50:26.305: D/dalvikvm(969): newInstance failed: p0 i1 [0 a1
 01-31 17:50:26.305: W/System.err(969): java.lang.InstantiationException: android.content.pm.IPackageManager
 01-31 17:50:26.305: W/System.err(969):     at java.lang.Class.newInstanceImpl(Native Method)
 01-31 17:50:26.315: W/System.err(969):     at java.lang.Class.newInstance(Class.java:1479)
 01-31 17:50:26.325: W/System.err(969):     at f.c.v.ClearUserDataUsingInterfaceActivity.onCreate(ClearUserDataUsingInterfaceActivity.java:52)
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-31 17:50:26.355: W/System.err(969):  at           android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
01-31 17:50:26.355: W/System.err(969):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
01-31 17:50:26.365: W/System.err(969):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
01-31 17:50:26.365: W/System.err(969):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
01-31 17:50:26.375: W/System.err(969):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 17:50:26.375: W/System.err(969):  at android.os.Looper.loop(Looper.java:123)
01-31 17:50:26.385: W/System.err(969):  at android.app.ActivityThread.main(ActivityThread.java:4363)
01-31 17:50:26.385: W/System.err(969):  at java.lang.reflect.Method.invokeNative(Native Method)
01-31 17:50:26.395: W/System.err(969):  at java.lang.reflect.Method.invoke(Method.java:521)
01-31 17:50:26.395: W/System.err(969):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-31 17:50:26.405: W/System.err(969):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

01-31 17:50:26.405:W/System.err(969):在dalvik.System.NativeStart.main(Native Method)

和全堆叠。

 01-31 17:26:38.285: E/AndroidRuntime(920): Uncaught handler: thread main exiting due to uncaught exception
 01-31 17:26:38.295: E/AndroidRuntime(920): java.lang.RuntimeException: Unable to start activity ComponentInfo{f.c.v/f.c.v.ClearUserDataUsingInterfaceActivity}: java.lang.NullPointerException
 01-31 17:26:38.295: E/AndroidRuntime(920):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
01-31 17:26:38.295: E/AndroidRuntime(920):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
01-31 17:26:38.295: E/AndroidRuntime(920):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
01-31 17:26:38.295: E/AndroidRuntime(920):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
01-31 17:26:38.295: E/AndroidRuntime(920):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 17:26:38.295: E/AndroidRuntime(920):  at android.os.Looper.loop(Looper.java:123)
01-31 17:26:38.295: E/AndroidRuntime(920):  at android.app.ActivityThread.main(ActivityThread.java:4363)
01-31 17:26:38.295: E/AndroidRuntime(920):  at java.lang.reflect.Method.invokeNative(Native Method)
01-31 17:26:38.295: E/AndroidRuntime(920):  at java.lang.reflect.Method.invoke(Method.java:521)
01-31 17:26:38.295: E/AndroidRuntime(920):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-31 17:26:38.295: E/AndroidRuntime(920):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-31 17:26:38.295: E/AndroidRuntime(920):  at dalvik.system.NativeStart.main(Native Method)
01-31 17:26:38.295: E/AndroidRuntime(920): Caused by: java.lang.NullPointerException
01-31 17:26:38.295: E/AndroidRuntime(920):  at java.lang.reflect.Method.invokeNative(Native Method)
1-31 17:26:38.295: E/AndroidRuntime(920):   at java.lang.reflect.Method.invoke(Method.java:521)
01-31 17:26:38.295: E/AndroidRuntime(920):  at f.c.v.ClearUserDataUsingInterfaceActivity.onCreate(ClearUserDataUsingInterfaceActivity.java:57)
01-31 17:26:38.295: E/AndroidRuntime(920):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-31 17:26:38.295: E/AndroidRuntime(920):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
01-31 17:26:38.295: E/AndroidRuntime(920):  ... 11 more

IPackageManager是一个接口(如前缀"I"所示)。不能通过反射实例化接口。

我猜你指的是android.content.pm.PackageManager

  1. 似乎应该调用myClass.newInstance(),而不是deleteClass.newInstances()
  2. 确保clearApplicationUserData()方法知道如何处理IPackageDataObserver的空值

希望能有所帮助。

什么是deleteClass?你不应该尝试在myClass上调用你的方法吗?

相关内容

  • 没有找到相关文章

最新更新