I am facing problem in android-ndk. When i try to call a java nan-static member function from cpp, i am not getting any runtime error as well, but function is not getting called.
But when i try calling a java static member function from cpp i am able to call succesfully, the member function definition is getting executed sucessfully.
/********** For static member function */
/* This is the c code */
jmethodID method = env->GetStaticMethodID(interfaceClass, "callBack", "(Ljava/lang/String;)V");
if(!method) {
LOGE("Callback_handler: Failed to get the callback method");
return;
}
env->CallStaticVoidMethod(interfaceClass, method, js);
/* This is the function in the java */
public static void callBack(String s) {
Bundle b = new Bundle();
b.putString("callback_string", s);
Message m = Message.obtain();
m.setData(b);
//Sending to the handler
h.sendMessage(m);
}
The above code works well, but the below code is not working
/********** For member function */
/* This is the c code */
jmethodID method = env->GetMethodID(interfaceClass, "callBack", "(Ljava/lang/String;)V");
LOGE("callback_handler: method %d", method);
if(!method) {
LOGE("Callback_handler: Failed to get the callback method");
return;
}
/* Call the callback function */
env->CallVoidMethod(interfaceClass, method, js);
/* This is the function in the java */
public void callBack(String s) {
Bundle b = new Bundle();
b.putString("callback_string", s);
Message m = Message.obtain();
m.setData(b);
//Sending to the handler
h.sendMessage(m);
}
Please let me know if i am missing anything.
Thanks & Regards,
SSuman185
During calling the member function, dont use the class instance as a the class parameter. Use the instance of the class as cls parameter. then we will be able to call the member function as well.
The answer is given by Selvin and it is working fine, but instead of answering he added the comments. So, i am updating the answer. Please give the credit to him.
Thanks & Regards,
SSuman185