调用方法时的NPE

时间:2012-01-31 12:06:40

标签: java android reflection methods

要删除应用程序数据,我尝试使用反射,但在调用方法时获取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

clearApplicationUserData不返回任何内容,并使用两个参数StringIPackageDataObserver

我传递了正确的参数吗?

或者我该如何解决这个问题?

调试时的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):at 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

3 个答案:

答案 0 :(得分:1)

IPackageManager是一个接口(由'I'前缀建议)。您无法通过反射实例化接口。

我猜你的意思是android.content.pm.PackageManager

答案 1 :(得分:0)

  1. 好像你应该调用myClass.newInstance()而不是deleteClass.newInstance()
  2. 确保clearApplicationUserData()方法知道处理IPackageDataObserver的空值。
  3. 希望它有所帮助。

答案 2 :(得分:0)

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