Calling Java Methods from C in Android
In the context of Java Native Interface (JNI) in Android, it is possible to invoke Java methods from C code during the execution of a native method called from Java. Here's a detailed analysis of a code snippet you provided, highlighting the issue and providing a solution:
Problem:
Your native code is attempting to call the "messageMe" method from the "the/package/MainActivity" class using the "CallObjectMethod" function. However, the error message "java.lang.NoSuchMethodError: messageMe" indicates that the method is not found.
Analysis:
The issue lies in the way you are passing the object to the "CallObjectMethod" function. In your code, you are passing the "jstr" object, which is a Java string, instead of the "obj" object, which is the instance of the MainActivity class.
Solution:
To call object methods from C , you need to pass the object itself to the "CallObjectMethod" function. The corrected code below shows the modification:
jobject result = env->CallObjectMethod(obj, messageMe, jstr);
Additional Notes:
By addressing the issue described above, you should be able to successfully call Java methods from C in your Android application.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3