简体   繁体   English

无法从Android中的本机函数调用无效的Java成员函数

[英]Not able to call a void java member function from the native function in android

I am facing problem in android-ndk. 我在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. 当我尝试从cpp调用Java nan-static成员函数时,我也未收到任何运行时错误,但未调用函数。
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. 但是,当我尝试从cpp调用Java静态成员函数时,我能够成功调用,该成员函数定义已成功执行。

/********** 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 苏曼185

During calling the member function, dont use the class instance as a the class parameter. 在调用成员函数期间,请勿将类实例用作类参数。 Use the instance of the class as cls parameter. 使用该类的实例作为cls参数。 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. 答案是由塞尔文(Selvin)提供的,并且工作正常,但他没有回答,而是添加了评论。 So, i am updating the answer. 因此,我正在更新答案。 Please give the credit to him. 请归功于他。

Thanks & Regards, 感谢和问候,
SSuman185 苏曼185

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM