Java/Android Call method from string WITH Value

android, dynamic, java, reflection

Solution

The instruction:

method.invoke (stringValue);

needs the object in which the `method` will be invoked.

So, if you try something like:

method = Class.forName("com.blah.MyActivity").getMethod('myFunction',String.class);
method.invoke(someInstanceOfMyActivity, stringValue);

it will work.

Documentation: http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/Method.html

Problem

Say I have a method that I wish to call which has an argument of a string To call, I would do something like this.. myFunction(stringValue); NOW, how would i make that same call, but dynamically if I had a string with the value of 'myFunction'.. something like ``` method = [convert "myFunction" string to method]; method.invoke(stringValue); ``` I am currently trying something like java.lang.reflect.Method method; ``` method = Class.forName("com.blah.MyActivity").getMethod('myFunction',String.class); method.invoke (stringValue); ``` but getting the error IllegalArgumentException Message expected receiver of type com.blah.MyActivity, but got java.lang.String

Original source