【问题标题】:Whats the meaning of Left of '-> NewGlobalRef' must point to struct/union?“-> NewGlobalRef”的左侧必须指向结构/联合是什么意思?
【发布时间】:2014-07-22 09:52:14
【问题描述】:

为了使用 JNI 实现对 java 代码的 C 回调,我遵循了 herehere 的建议,并调整了我的本机方法实现以存储对稍后回调所需对象的引用:

JavaVM * g_vm;
jobject g_obj;
jmethodID g_mid;

//More code here

JNIEXPORT jboolean JNICALL
Java_a_B_initialize(JNIEnv * env, jobject obj)
{
    g_obj = env->NewGlobalRef(obj);
    jclass g_clazz = env->GetObjectClass(g_obj);
    if (g_clazz == NULL) {
        printf("Failed to find class");
    }

    g_mid = env->GetMethodID(g_clazz, "callback", "(I)V");
    if (g_mid == NULL) {
        printf("Unable to get method ref");
    }

但是,这无法编译,我收到错误消息:

Left of '-> NewGlobalRef' must point to struct/union
Left of '-> GetObjectClass' must point to struct/union
Left of '-> GtMethodID' must point to struct/union

我不明白这些错误信息。我的代码有什么问题?

【问题讨论】:

  • 您是否忘记了#include
  • 我的包含看起来像这样:#include #include #include #include #include "a_B.h" 应该还有更多吗?
  • 你能粘贴编译器的完整输出吗?

标签: java c java-native-interface


【解决方案1】:

您正在尝试使用 C++ 接口。您已经标记了问题 [C],所以我假设您正在使用 C。

在 C 中,这样做:

(*env)->NewGlobalRef(env, obj)

【讨论】:

    【解决方案2】:

    您缺少 JNI 包括:

    #include <jni.h>
    

    【讨论】:

    • 我添加了 include 语句,但仍然收到完全相同的错误消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多