I want to call a non-static method on Android using JNI. I can call static methods using CallStaticVoidMethod. To call non-static methods, I have used CallVoidMethod. It is not working.
Can anybody please tell me correct code to call nonStatic method of Android From JNI?
jmethodID method = env->GetMethodID(gJniRefCached.ImsFwkLoaderClass, "DispVideo", "([BII)V");
env->CallVoidMethod(gJniRefCached.ImsFwkLoaderClass, method,arr,width,height);
I have also tried using object of class that code is
jclass cls = env->GetObjectClass(obj);
jmethodID method = env->GetMethodID(cls, "DispVideo", "([BII)V");
env->CallVoidMethod(cls, method,arr,width,height);
In order to call an instance method, you need to provide an instance of the class the method belng to, represented as an
jobject. However, in both examples you are trying to call the instance method with an instance of the class definition, represented as ajclass.Try the following:
Note the subtle difference in the third line of code, where I use
objas the first parameter, instead ofcls.You can see this difference also on the documentation page for the instance method JNI functions: http://download.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html#wp16656
Look at both
GetMethodIDandCall<type>Method– one takesjclass, the other takesjobject.