【问题标题】:Trouble calling on a Java method from a native thread using JNI (C++)使用 JNI (C++) 从本机线程调用 Java 方法时遇到问题
【发布时间】:2011-09-12 20:50:11
【问题描述】:

我有一个 JNI 问题,希望有人能帮我解决。
我正在尝试从本机线程调用名为 LUSOutputJNI 的 Java 类的构造函数。
它在这个特定类的 FindClass(...) 上一直失败。

代码如下:

 LOGE("1");
    JNIEnv *env = NULL;

    LOGE("2");
    int res = -1;
    res = g_vm->AttachCurrentThread(&env, NULL);

    if(env == NULL)
    {
        LOGE("env is NULL, AttachCurrentThread failed");;
    }


    if(res >= 0)
        LOGE("AttachCurrentThread was successful");
    jclass clazz = NULL;
    jmethodID cid;

    jclass clazzESEngine;
    jmethodID callbackid;

    jobject jCoreOut;
    static jfieldID fid_ActionState = NULL;
    static jfieldID fid_nSpeed = NULL;
    static jfieldID fid_nType = NULL;
    static jfieldID fid_nInProcess = NULL;
    static jfieldID fid_nX = NULL;
    static jfieldID fid_nY = NULL;
    LOGE("3");

    static const char* const ECOClassName = "lus/android/sdk/LUSOutputJNI";
    //static const char* const ECOClassName = "android/widget/TextView";
    clazz = env->FindClass(ECOClassName);
    if (clazz == NULL) {
        LOGE("Can't find class LUSOutputJNI");

    }
    else
        LOGE("lus/android/sdk/LUSOutputJNI was found, YEY!!");

    LOGE("4");
    cid = env->GetMethodID(clazz,"<init>", "()V");
    LOGE("5");
    jCoreOut = env->NewObject(clazz, cid);

    LOGE("6");    

这是失败时的 logcat 输出:

E/lusCore_JNI( 3040): 1
E/lusCore_JNI( 3040): 2
E/lusCore_JNI( 3040): AttachCurrentThread was successful
E/lusCore_JNI( 3040): 3
E/lusCore_JNI( 3040): Can't find class LUSOutputJNI
E/lusCore_JNI( 3040): 4
W/dalvikvm( 3040): JNI WARNING: JNI method called with exception raised  

观察:

  • 我从 AttachCurrentThread 得到的结果是 0,这意味着这个附件是成功的 + env 指针不再是 NULL。
  • 我确定 LUSOutputJNI 的包名声明(三重检查...)
  • 当我尝试使用更流行的类名(例如 android/widget/TextView )运行 FindClass(..) 时,我得到了肯定的匹配。它就在那里。意味着线程附件和环境变量都可以。 (我可以假设吗?)
  • 当我尝试从运行有 JNI 线程的 JNI 方法运行以下代码时,它发现 LUSOutputJNI 类没有问题。

我做错了什么?

我们将不胜感激:)

感谢您的宝贵时间,

意大利

【问题讨论】:

  • 对这个“LUSOutputJNI”类的任何引用?
  • @K-ballo:可能是内部课程。
  • @Ita:您的 LUSOutputJNI 静态初始化程序是否抛出异常?您应该打印引发的异常。
  • @lta 你应该修复你的错误处理。这让我哭了。
  • LUSOutputJNI 在类路径中吗?

标签: c++ java-native-interface android-ndk


【解决方案1】:

找到答案\在此处解决。 (查找常见问题解答:“FindClass 没有找到我的课程” JNI tips)

我基本上保存了一个全局引用到所需的 jclass 对象。
然而,为了编译代码,必须克服 C/JNI 和 C++/JNI 之间的一些邪恶的 JNI 更改。
这就是我让 NewGlobalRef 编译和工作的方式。

jclass localRefCls = env->FindClass(strLUSClassName);
if (localRefCls == NULL) {
    LOGE("Can't find class %s",strLUSCoreClassName);
    return result;
}

//cache the EyeSightCore ref as global
 /* Create a global reference */
 clazzLUSCore = (_jclass*)env->NewGlobalRef(localRefCls);

 /* The local reference is no longer useful */
 env->DeleteLocalRef(localRefCls);

 /* Is the global reference created successfully? */
 if (clazzLUSCore == NULL) {
     LOGE("Error - clazzLUSCore is still null when it is suppose to be global");
     return result; /* out of memory exception thrown */
 }  

我希望这对任何人都有帮助。

【讨论】:

【解决方案2】:

要获得更多关于哪里出了问题的信息(它将记录到 stderr,而不是 LOGE,我不知道如何更改它)您可以添加一些异常打印 - 您可以更改此:

//static const char* const ECOClassName = "android/widget/TextView";
clazz = env->FindClass(ECOClassName);
if (clazz == NULL) {
    LOGE("Can't find class LUSOutputJNI");

}
else
    LOGE("lus/android/sdk/LUSOutputJNI was found, YEY!!");

到这里:

    //static const char* const ECOClassName = "android/widget/TextView";
    clazz = env->FindClass(ECOClassName);
    if (clazz == NULL) {
        LOGE("Can't find class LUSOutputJNI");
        jthrowable exc = env->ExceptionOccurred();
        if(exc)
        {
          env->ExceptionDescribe();
          env->ExceptionClear();
        }    
    }
    else
        LOGE("lus/android/sdk/LUSOutputJNI was found, YEY!!");

【讨论】:

  • 看起来您可以使用 jthrowable 中的 toString 函数获取错误信息,然后将其转换为 C 字符串并将其放入您的 LOGE 函数中,但这对于调试它可能是多余的.
猜你喜欢
  • 2011-09-28
  • 2012-01-02
  • 1970-01-01
  • 2012-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
  • 2012-06-16
相关资源
最近更新 更多