JNI NoSuchMethodException:参数类型为 null
我正在努力在 Android
中使用 JNI
调用实例方法。所有代码均已编译,但为了保护项目内容,我仅展示代码的相关部分。以下代码块(在 c++ 文件中使用 extern c
)尝试访问 public String getMyString()] 中的静态方法>cls。我知道 cls 和 env 不为空。 mid 未设置为零,也不是 null。当这个本机方法返回到Java端时,我收到以下错误(追溯到Class.java):
02-22 10:38:36.010: E/AndroidRuntime(17894): FATAL EXCEPTION: GLThread 10
02-22 10:38:36.010: E/AndroidRuntime(17894): java.lang.NoSuchMethodException: parameter type is null
02-22 10:38:36.010: E/AndroidRuntime(17894): at java.lang.ClassMembers.getConstructorOrMethod(ClassMembers.java:228)
02-22 10:38:36.010: E/AndroidRuntime(17894): at java.lang.Class.getMethod(Class.java:904)
02-22 10:38:36.010: E/AndroidRuntime(17894): at com.example.android.MyClass.getMyString(Native Method)
02-22 10:38:36.010: ...
生成错误的代码如下:
jmethodID mid = env->GetMethodID(cls, "getMyString", "()Ljava/lang/String;");
if (mid == 0) {
__android_log_write(ANDROID_LOG_INFO, "JNI", "mid is 0");
}
else {
__android_log_write(ANDROID_LOG_INFO, "JNI", "mid is not 0");
}
jstr = (jstring) env->CallObjectMethod(obj, mid);
if (jstr == NULL) {
__android_log_write(ANDROID_LOG_INFO, "JNI", "jstr is NULL");
}
else {
__android_log_write(ANDROID_LOG_INFO, "JNI", "jstr is not NULL");
}
printf(" c.s = \"%s\"\n", env->GetStringUTFChars(jstr, NULL));
return jstr;
虽然 Class.java 似乎表明这是从 null 到Class[]
(反射常见),我有一种感觉,它更多地与mid找到错误的方法或jstr有关> 错误返回(logcat 指出 jstr 不为空)。
最后,Java端相关代码如下:
原生调用:
public static native String getMyNativeString();
调用原生方法:
String myStr = callGetMyNativeString();
正在访问的方法:
public String getMyString() {
return "Foobar";
}
I am working on calling an instance method using JNI
in Android
. All code compiles, yet to protect the content of the project, I only will show relevant parts of the code. The following code block (extern c
used in a c++ file) attempts to access a static method [public String getMyString()
] in cls. I know that cls and env are not null. mid is not set to zero, nor is it null. When this native method returns to the Java side, I get the following error (traced back to Class.java):
02-22 10:38:36.010: E/AndroidRuntime(17894): FATAL EXCEPTION: GLThread 10
02-22 10:38:36.010: E/AndroidRuntime(17894): java.lang.NoSuchMethodException: parameter type is null
02-22 10:38:36.010: E/AndroidRuntime(17894): at java.lang.ClassMembers.getConstructorOrMethod(ClassMembers.java:228)
02-22 10:38:36.010: E/AndroidRuntime(17894): at java.lang.Class.getMethod(Class.java:904)
02-22 10:38:36.010: E/AndroidRuntime(17894): at com.example.android.MyClass.getMyString(Native Method)
02-22 10:38:36.010: ...
The code that generates the error is as follows:
jmethodID mid = env->GetMethodID(cls, "getMyString", "()Ljava/lang/String;");
if (mid == 0) {
__android_log_write(ANDROID_LOG_INFO, "JNI", "mid is 0");
}
else {
__android_log_write(ANDROID_LOG_INFO, "JNI", "mid is not 0");
}
jstr = (jstring) env->CallObjectMethod(obj, mid);
if (jstr == NULL) {
__android_log_write(ANDROID_LOG_INFO, "JNI", "jstr is NULL");
}
else {
__android_log_write(ANDROID_LOG_INFO, "JNI", "jstr is not NULL");
}
printf(" c.s = \"%s\"\n", env->GetStringUTFChars(jstr, NULL));
return jstr;
Although Class.java seems to suggest it is a problem with the cast from null to Class<?>[]
(common for reflection), I have a feeling it has more to do with mid finding the wrong method or jstr coming back incorrectly (logcat states that jstr is NOT null).
Lastly, the Java-side relevant code is as follows:
The native call:
public static native String getMyNativeString();
The call to the native method:
String myStr = callGetMyNativeString();
The method that is being accessed:
public String getMyString() {
return "Foobar";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有看到完整的 java 代码,我无法确定这一点。但是本机方法被定义为静态,而 java 方法 getMyString() 是非静态的。那么,如何从 JNI 获取调用
getMyString()
所需的obj
呢?I can't be sure of this without seeing the full java code. But the native method is being defined as static, and the java method
getMyString()
is non-static. So from the JNI, how did you get theobj
needed to callgetMyString()
?