NoSuchMethodError on android View method in JNI GetMethodID

huangapple go评论78阅读模式
英文:

NoSuchMethodError on android View method in JNI GetMethodID

问题

我正试图通过JNI在Android类android.view.View上获取一些methodID。我已经成功通过JNI获取了许多其他方法,但是这个方法(setOnClickListener),以及我尝试获取的另一个方法(setLayoutParams),它们都是在android.view.View中实现的,但无法被JNI找到。以下是我使用的代码,日志显示找不到该方法,然后出现noSuchMethodError异常。

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", "无法找到 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", "无法找到 setOnClickListener 方法");
        return -1;
    }
    return 0;
}

这是否与View类有关?我该如何成功获取这个方法?

英文:

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)-&gt;GetEnv(vm, (void **) &amp;env, JNI_VERSION_1_4);
    if (status &lt; 0) {
        status = (*vm)-&gt;AttachCurrentThread(vm, (void **) &amp;env, NULL);
        if (status &lt; 0) { return -1; }
    }

    jclass cView = (*env)-&gt;FindClass(env, &quot;android/view/View&quot;);

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

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

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

答案1

得分: 1

类名必须使用$符号分隔。例如:android/view/View$OnClickListener

我想你得到null是因为格式不正确。

修正后的mSetOnClickListener声明:

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

同样的情况也适用于setLayoutParams,因为它接受内部类ViewGroup.LayoutParams的实例作为参数。

英文:

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)-&gt;GetMethodID(env, cView, &quot;setOnClickListener&quot;, &quot;(Landroid/view/View$OnClickListener;)V&quot;); 

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

huangapple
  • 本文由 发表于 2020年7月24日 17:32:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63070834.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定