JNIEXPORT jobject JNICALL Java_com_example_androidhellojni_FooFragmentTab_getUserList(JNIEnv *env, jobject obj)
{
jint i;
jclass cls_arraylist = (*env)->FindClass(env, "java/util/ArrayList");
jmethodID init_arraylist = (*env)->GetMethodID(env, cls_arraylist, "<init>", "()V");
jobject obj_arraylist = (*env)->NewObject(env, cls_arraylist, init_arraylist, "");
if (obj_arraylist == NULL) LOGD("obj_arrlist fail");
jmethodID arraylist_add = (*env)->GetMethodID(env, cls_arraylist, "add", "(Ljava/lang/Object;)Z");
if (arraylist_add == NULL) LOGD("arraylist_add fail");
jclass cls_int = (*env)->FindClass(env, "java/lang/Integer");
jmethodID init_int = (*env)->GetMethodID(env, cls_int, "<init>", "(I)V");
for (i = 0; i < 10; i++) {
jobject obj_int = (*env)->NewObject(env, cls_int, init_int, i);
(*env)->CallObjectMethod(env, obj_arraylist, arraylist_add, obj_int);
}
return obj_arraylist;
}
This is my sample code to get return ArrayList from C to Java(Android), but compiled and run there exist some error message like:
art/runtime/check_jni.cc:65] JNI DETECTED ERROR IN APPLICATION: the return type of CallObjectMethod does not match boolean java.util.ArrayList.add(java.lang.Object)
art/runtime/check_jni.cc:65] in call to CallObjectMethod
art/runtime/check_jni.cc:65] from java.util.ArrayList com.example.androidhellojni.FooFragmentTab.getUserList()
The error message is pretty clear about what the problem is:
the return type of CallObjectMethod does not match boolean java.util.ArrayList.add(java.lang.Object)
The type
in Call<type>Method
refers to the type of the method, not the type of the method's argument(s). And the type of the method is boolean
, which is not an Object
.
Hence you should use CallBooleanMethod
to call arraylist_add
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With