Hi Everyone,

I have an issue where (*env)->CallStaticVoidMethod is crashing. I am
caching the jvm, class and method id on JNI_OnLoad. In JNI_OnLoad i
start a thread! in that thread I want to call back a java method which
I am having problems with. Also should I be using JNI_VERSION_1_2 or
JNI_VERSION_1_4?

JavaVM *cached_jvm;
jclass Class_C;
jmethodID MID_C_callback;


JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *jvm, void *reserved)
{
        __android_log_print(ANDROID_LOG_DEBUG, "JNI_OnLoad",
                        "JNI_OnLoad - start");

        JNIEnv *env;
        jclass cls;
        // cache the jvm
        cached_jvm = jvm;

        if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_2)) {
                __android_log_print(ANDROID_LOG_DEBUG, "JNI_OnLoad",
                                "ERROR: GetEnv failed ");

                return JNI_ERR; // JNI version not supported;
        }

        cls = (*env)->FindClass(env,
                        "com/bla/bla/xxxActivity");

        if (cls == NULL) {
                __android_log_print(ANDROID_LOG_DEBUG, "JNI_OnLoad",
                                "ERROR: FindClass failed");

                return JNI_ERR;
        }

        /* Use weak global ref to allow C class to be unloaded */
        Class_C = (*env)->NewWeakGlobalRef(env, cls);
        if (Class_C == NULL) {
                __android_log_print(ANDROID_LOG_DEBUG, "JNI_OnLoad",
                                "ERROR; NewWeakGlobalRef failed ");

                return JNI_ERR;
        }

        /* Compute and cache the method ID */
        MID_C_callback = (*env)->GetStaticMethodID(env, cls, "callback",
"()V");
        if (MID_C_callback == NULL) {
                __android_log_print(ANDROID_LOG_DEBUG, "JNI_OnLoad",
                                                "ERROR:GetStaticMethod failed 
");
                return JNI_ERR;
        }

        startMyThread();

        return JNI_VERSION_1_2;
}

void startMyThread()
{
        pthread_t myThread;
        int rc;
        rc = pthread_create(&myThread, NULL, (void *) &workerFunction, NULL);
        if (rc) {
                __android_log_print(ANDROID_LOG_DEBUG, GENERAL_LOG,
                                "ERROR starting pulse detection thread ");
        }
}

static void workerFunction()
{
        JNIEnv *env;
        int status = (*cached_jvm)->AttachCurrentThread(cached_jvm, &env,
NULL);
        // status is 0 which means success
        (*env)->CallStaticVoidMethod(env, Class_C, MID_C_callback); //
this crashes

}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to