【问题标题】:Android NDK get ArrayList errorAndroid NDK 获取 ArrayList 错误
【发布时间】:2015-08-28 03:30:43
【问题描述】:
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 到 Java(Android) 的返回 ArrayList 的示例代码,但是编译和运行存在一些错误消息,例如:

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()

【问题讨论】:

    标签: android arraylist android-ndk


    【解决方案1】:

    错误消息很清楚问题是什么:

    the return type of CallObjectMethod does not match boolean java.util.ArrayList.add(java.lang.Object)

    Call&lt;type&gt;Method 中的type 指的是方法的类型,而不是方法参数的类型。并且方法的类型是boolean,不是Object

    因此您应该使用CallBooleanMethod 调用arraylist_add

    【讨论】:

      猜你喜欢
      • 2018-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-28
      相关资源
      最近更新 更多