I have 2 classes in my android app. Class1 and Class2. I have some methods in class2 which i want to invoke using reflection API.
To invoke “printString” of class2, i am using following code:
public void printString(String str) {
Log.d("TAG", "in printString" + str);
}
Reflection Code:
Method method = Class2.class.getMethod("printString");
method.invoke("asjdhaskdf", null);
But using this code is throwing following exception:
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)
Please advise what i am doing wrong. I have read tutorials available on internet but nothing worked.
Update 1
One Imp Point: My code never reached on “method.invoke” line. It throws an exception on the line where i am getting the method using “getMethod” function.
Code of Class2:
package com.sample.mobile.android.utils;
public class Class2{
public void printString(String str) {
Log.d("TAG", "in printString" + str);
}
}
Code of 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();
}
}
}
it because there is no such method
you’re trying invoke this method on String class
it should be
method.invoke(instanceofClass2, "asjdhaskdf")EDIT: to match Update 1
… and since
printStringhas String parameters you should usegetMethodlike this: