Ad

NoSuchMethodError On Android View Method In JNI GetMethodID

I am trying to get some methodID through JNI on the Android class android.view.View. I managed to get a lot of other methods through JNI but this one, as well as another method I tried to get (setLayoutParams) that are implemented in android.view.View cannot be found by JNI. Here is the code that I use, and I end up with the log that says can not find the method. Also then a noSuchMethodError exception.

jint JNI_OnLoad(JavaVM *vm, void *reserved) {
    JNIEnv *env;
    int status;

    status = (*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_4);
    if (status < 0) {
        status = (*vm)->AttachCurrentThread(vm, (void **) &env, NULL);
        if (status < 0) { return -1; }
    }

    jclass cView = (*env)->FindClass(env, "android/view/View");

    if (cView == NULL) {
        __android_log_write(ANDROID_LOG_DEBUG, "JNI", "can not find the class View ");
        return -1;
    }

    jmethodID mSetOnClickListener = (*env)->GetMethodID(env, cView, "setOnClickListener", "(Landroid/view/View/OnClickListener;)V");
    if (mSetOnClickListener == NULL) {
        __android_log_write(ANDROID_LOG_DEBUG, "JNI", "can not find the method setOnClickListener");
        return -1;
    }
    return 0;
}

Does it have something to do with the View class ? How can I manage to get this method ?

Ad

Answer

Class names must be separated using $ sign. Example: android/view/View$OnClickListener

I suppose you are getting null because of the wrong format.

Fixed mSetOnClickListener declaration:

jmethodID mSetOnClickListener = (*env)->GetMethodID(env, cView, "setOnClickListener", "(Landroid/view/View$OnClickListener;)V"); 

Same happens to setLayoutParams because it accepts an inner class ViewGroup.LayoutParams instance as an argument.

Ad
source: stackoverflow.com
Ad