收到回调后,从JNI调用Java方法

时间:2015-04-15 11:37:57

标签: java android callback java-native-interface ansi-c

我正在构建一个JNI应用程序。

从我的JNI代码中,我想调用更新UI的java函数。

基本上当我收到回调并尝试获取对环境的引用时,我收到类型错误: Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 1869 (ALooper)

我已经读过,对env进行静态引用并不好,但我可以对JavaVM进行静态引用

你能告诉我我做错了什么吗?

非常感谢你的时间

jclass globalClass = NULL;
static JavaVM* mVm = NULL;

jint JNI_OnLoad(JavaVM* vm, void* reserved) {
    mVm = vm;
    JNIEnv* env;

    if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_6) != JNI_OK) {
        __android_log_print(ANDROID_LOG_DEBUG, "nativeTAG", "get env failed");
        return -1;
    }
    return JNI_VERSION_1_6;
}

static void callMyJavaFunction() {
    JNIEnv* env;
    jclass playerUtils = NULL;
    jmethodID updatePlayerStatus = NULL;
    jobject myObject = NULL;

    if ((*mVm)->GetEnv(mVm, (void **) &env, JNI_VERSION_1_6) != JNI_OK) {
        __android_log_print(ANDROID_LOG_DEBUG, "jajaja", ">>NATIVE jni failed");

        /*ERROR A/libc(1853): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 1869 (ALooper)*/
    }

    playerUtils = (*env)->FindClass(env,
        "com/example/customviewcircles/nativeaudio/PlayerUtils");

    if (playerUtils != NULL) {
        updatePlayerStatus = (*env)->GetMethodID(env, playerUtils,
            "updatePlayerStatus", "()V");

        if (updatePlayerStatus != NULL) {
            myObject = (*env)->NewObject(env, playerUtils, updatePlayerStatus);

            (*env)->CallVoidMethod(env, myObject, updatePlayerStatus);
        }
    }
 }

 void myCallBackFunction() {
     callMyJavaFunction();
 }

1 个答案:

答案 0 :(得分:2)

一位聪明的伴侣告诉我我的方式。

如果有人需要它:

jclass globalClass = NULL;
static JavaVM* mVm = NULL;

jint JNI_OnLoad(JavaVM* vm, void* reserved) {

    mVm = vm;

    JNIEnv* env;
    jclass playerUtils = NULL;

    if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_6) != JNI_OK) {
        __android_log_print(ANDROID_LOG_DEBUG, "jajaja", ">>NATIVE jni failed");
        return -1;
    }
    playerUtils = (*env)->FindClass(env,
            "com/example/customviewcircles/nativeaudio/PlayerUtils");

    if (playerUtils != NULL) {

        globalClass = (*env)->NewGlobalRef(env, playerUtils);
    }
    return JNI_VERSION_1_6;
}

void callMyJavaFunction() {

    JNIEnv* env;
    jmethodID updatePlayerStatus = NULL;
    jobject myObject = NULL;

    if((*mVm)->AttachCurrentThread(mVm, &env, NULL) == JNI_OK) {
        __android_log_print(ANDROID_LOG_DEBUG, "jajaja", ">>NATIVE attached successfully");
    }

    if (globalClass != NULL) {

        __android_log_print(ANDROID_LOG_DEBUG, "jajaja",
                            ">>NATIVE globalclass not null");

        updatePlayerStatus = (*env)->GetMethodID(env, globalClass,
                "updatePlayerStatus", "()V");

        if (updatePlayerStatus != NULL) {

            myObject = (*env)->NewObject(env, globalClass, updatePlayerStatus);
        }
    }
    (*mVm)->DetachCurrentThread(mVm);

}

 void myCallBackFunction() {
     callMyJavaFunction();
 }