Android NDK获取ArrayList错误
Android NDK get ArrayList error
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;
}
这是我从 C 获取 return ArrayList 到 Java(Android) 的示例代码,但编译后 运行 存在一些错误消息,如:
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 return type of CallObjectMethod does not match boolean java.util.ArrayList.add(java.lang.Object)
Call<type>Method
中的type
是指方法的类型,而不是方法参数的类型。方法的类型是 boolean
,不是 Object
.
因此您应该使用 CallBooleanMethod
来调用 arraylist_add
。
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;
}
这是我从 C 获取 return ArrayList 到 Java(Android) 的示例代码,但编译后 运行 存在一些错误消息,如:
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 return type of CallObjectMethod does not match boolean java.util.ArrayList.add(java.lang.Object)
Call<type>Method
中的type
是指方法的类型,而不是方法参数的类型。方法的类型是 boolean
,不是 Object
.
因此您应该使用 CallBooleanMethod
来调用 arraylist_add
。