JNI多个registernatives电话

时间:2015-03-02 22:31:14

标签: android c++ qt android-ndk java-native-interface

请参阅以下代码段。基本上我有两个我希望将本机方法与之关联的Java类。我在“clazz”和“clazz2”之间切换jclass对象名称。无论哪一个被设置为clazz2都无法正确加载并导致应用程序崩溃,因为缺少本机调用。关于为什么第二次调用RegisterNatives总是失败的任何想法?

为了澄清,只需一个指向NotificationClient的jclass和一个registerNatives调用就可以正常工作。

static JNINativeMethod methods[] = {
    {"keyboardVisibleChanged", "(ZIIII)V", (void *)keyboardVisibleChanged},
    {"androidBackPressed", "()V", (void *)androidBackPressed},
    {"activityResultFinished", "(Ljava/lang/String;)V", (void *)activityResultFinished},
    {"activityResultTextFinished", "(Ljava/lang/String;Ljava/lang/String;)V", (void *)activityResultTextFinished},
    {"activityResultFormFinished", "(Ljava/lang/String;)V", (void *)activityResultFormFinished},
    {"activityResultCanceled", "()V", (void *)activityResultCanceled}
};

static JNINativeMethod syncServiceMethods[] = {
    {"blah", "()V", (void *)blah}
};

jint JNICALL JNI_OnLoad(JavaVM *vm, void *)
{
    JNIEnv *env;
    if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_4) != JNI_OK)
        return JNI_FALSE;

    jclass clazz2 = env->FindClass("org/qtproject/example/notification/NotificationClient");
    jclass clazz = env->FindClass("org/qtproject/example/notification/SyncService");
    env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
    env->RegisterNatives(clazz2, syncServiceMethods, sizeof(syncServiceMethods) / sizeof(syncServiceMethods[0]));

return JNI_VERSION_1_4;
}

编辑:当打印出两个env->RegisterNative调用的返回值时,两者都返回0,这与成功运行应用程序时返回的值相同,只需要RegisterNatives个调用。

1 个答案:

答案 0 :(得分:0)

更改实施方式。

当FindClass不为null时,它不会循环调用,而是循环调用。