JNI NoSuchMethodException:参数类型为 null

发布于 2025-01-08 02:56:50 字数 2097 浏览 0 评论 0原文

我正在努力在 Android 中使用 JNI 调用实例方法。所有代码均已编译,但为了保护项目内容,我仅展示代码的相关部分。以下代码块(在 c++ 文件中使用 extern c)尝试访问 public String getMyString()] 中的静态方法>cls。我知道 clsenv 不为空。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

知足的幸福 2025-01-15 02:56:50

如果没有看到完整的 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 the obj needed to call getMyString()?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文