[英]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;
}
This is my sample code to get return ArrayList from C to Java(Android), but compiled and run there exist some error message like: 这是我的示例代码,用于将ArrayList从C返回到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 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). 该
type
在Call<type>Method
指的是方法,而不是方法的参数(一个或多个)的类型的类型。 And the type of the method is boolean
, which is not an Object
. 并且方法的类型是
boolean
,它不是Object
。
Hence you should use CallBooleanMethod
to call arraylist_add
. 因此,您应该使用
CallBooleanMethod
来调用arraylist_add
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.