taraloca
taraloca

Reputation: 9135

How to Use Method via Reflection Java

I am trying to use reflection instead of including a jar file. I want to do this to reduce my apk size when I am not using the jar code...but if I include the code, I must include the jar. That being said, I am trying to grasp reflection and not doing well.

I have this code to see the methods available (and it works):

//initialize method params
Class[] paramInitialize = new Class[2]; 
paramInitialize[0] = Activity.class;
paramInitialize[1] = String.class;

try {
    Logger.i(DEBUG_TAG, "try statement");
    Class <?> tremorVideoCls = Class.forName("com.tremorvideo.sdk.android.videoad.TremorVideo");
    Class <?> tremorSettingsCls = Class.forName("com.tremorvideo.sdk.android.videoad.Settings");
    Object object = tremorVideoCls.newInstance();
    String apkName = this.getPackageManager().getApplicationInfo(this.getPackageName(), 0).sourceDir;
    loader = new dalvik.system.PathClassLoader(apkName, ClassLoader.getSystemClassLoader());
    tremorVideoCls = loader.loadClass("com.tremorvideo.sdk.android.videoad.TremorVideo");
    Method m[] = tremorVideoCls.getMethods();
    Method declaredM[] = tremorVideoCls.getDeclaredMethods();
    for (int i = 0; i < m.length; i++) {
        Logger.i(DEBUG_TAG, "getMethods is " + m[i].toString());
    }
    Field []field = tremorSettingsCls.getFields();
    for (int i = 0; i < field.length; i++) {
        Logger.i(DEBUG_TAG, "getFields is " + field[i].toString());
    }
    Method tremorInitialize = tremorVideoCls.getMethod("initialize", paramInitialize);
    Logger.i(DEBUG_TAG, "tremorInitialize is " + tremorInitialize.toGenericString());
//  tremorInitialize.invoke(object, this, ACCUWX.TREMOR_ADSPACE_TEST);

    } catch (ClassNotFoundException e) {
    } catch (NoSuchMethodException x) {
 ...

BUT, I get a logcat error throwing an exception:

08-06 11:48:51.134: W/System.err(11688): java.lang.NoSuchMethodException: initialize [class android.app.Activity, class java.lang.String]

Here is the logcat printing out that method:

08-06 11:48:51.118: I/Flagship/MainActivity(11688): getMethods is public static void com.tremorvideo.sdk.android.videoad.TremorVideo.initialize(android.content.Context,java.lang.String)

I'm not sure what do try next...any help greatly appreciated.

Upvotes: 0

Views: 1201

Answers (2)

Peter
Peter

Reputation: 1789

The issue is not the invocation of the method but the call to retreiving the method.
Because the Class[] is initialized with Activity.class, a method with the signature
void initialize(Activity activity, String string) is searched for.

This method doesn't exist.
So it should be

 paramInitialize[0] = Context.class; 

to find the method

initialize(Contex ctxt, String arg).

Upvotes: 0

Bharat Sinha
Bharat Sinha

Reputation: 14363

If the method which you are trying to invoke is

public static void com.tremorvideo.sdk.android.videoad.TremorVideo.initialize(android.content.Conte‌​xt,java.lang.String)

and the error is

08-06 11:48:51.134: W/System.err(11688): java.lang.NoSuchMethodException: initialize [class android.app.Activity, class java.lang.String]

It clearly indicates that the method expects android.content.Conte‌​xt but what you are passing android.app.Activity

In the call

tremorInitialize.invoke(object, this, ACCUWX.TREMOR_ADSPACE_TEST);

instead of passing this you should pass an instance of android.content.Conte‌​xt.

Hope this helps!!!

Upvotes: 1

Related Questions