英文:
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)->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 ?
答案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)->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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论