我的 android 应用程序中有 2 个类。Class1
和Class2
。我在 class2 中有一些方法,我想使用反射 API 调用它们。
要调用 class2 的“printString”,我使用以下代码:
public void printString(String str) {
Log.d("TAG", "in printString" + str);
}
反射代码:
Method method = Class2.class.getMethod("printString");
method.invoke("asjdhaskdf", null);
但是使用此代码会引发以下异常:
10-26 16:10:16.931: W/System.err(25036): java.lang.NoSuchMethodException: printString []
10-26 16:10:16.931: W/System.err(25036): at java.lang.Class.getConstructorOrMethod(Class.java:460)
10-26 16:10:16.931: W/System.err(25036): at java.lang.Class.getMethod(Class.java:915)
10-26 16:10:16.931: W/System.err(25036): at com.sample.mobile.android.ui.activities.Class1Activity$1.onClick(Class1Activity.java:56)
10-26 16:10:16.931: W/System.err(25036): at android.view.View.performClick(View.java:3627)
10-26 16:10:16.931: W/System.err(25036): at android.view.View$PerformClick.run(View.java:14329)
10-26 16:10:16.936: W/System.err(25036): at android.os.Handler.handleCallback(Handler.java:605)
10-26 16:10:16.936: W/System.err(25036): at android.os.Handler.dispatchMessage(Handler.java:92)
10-26 16:10:16.936: W/System.err(25036): at android.os.Looper.loop(Looper.java:137)
10-26 16:10:16.941: W/System.err(25036): at android.app.ActivityThread.main(ActivityThread.java:4511)
10-26 16:10:16.941: W/System.err(25036): at java.lang.reflect.Method.invokeNative(Native Method)
10-26 16:10:16.941: W/System.err(25036): at java.lang.reflect.Method.invoke(Method.java:511)
10-26 16:10:16.941: W/System.err(25036): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:986)
10-26 16:10:16.941: W/System.err(25036): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:753)
10-26 16:10:16.941: W/System.err(25036): at dalvik.system.NativeStart.main(Native Method)
请告知我做错了什么。我已经阅读了互联网上可用的教程,但没有任何效果。
更新 1
一个提示点:我的代码从未到达“method.invoke”行。它在我使用“getMethod”函数获取方法的行上引发异常。
Class2代码:
package com.sample.mobile.android.utils;
public class Class2{
public void printString(String str) {
Log.d("TAG", "in printString" + str);
}
}
class1Activity 的代码:
package com.sample.mobile.android.ui.activities;
public class Class1Activity extends Activity {
private Class2 obj;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
obj = new Class2();
Class cls;
try {
cls = Class
.forName("com.sample.mobile.android.utils.Class2");
Method[] methods = cls.getMethods();
Log.d("TAG", "length -- " + methods.length);
for (int i = 0; i < methods.length; i++) {
Log.d("TAG", "name -- " + methods[i].getName());
// this is printing the name of all methods including "printString".
}
Method method = cls.getMethod("printString");
method.invoke(obj, "asjdhaskdf");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}