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:
<code class="c++">jobject result = env->CallObjectMethod(obj, messageMe, jstr);</code>
Additional Notes:
By addressing the issue described above, you should be able to successfully call Java methods from C in your Android application.
The above is the detailed content of How To Correctly Call Java Methods From C in Android Using JNI?. For more information, please follow other related articles on the PHP Chinese website!