故障使用JNI调用Java方法从本地线程(C ++) [英] Trouble calling on a Java method from a native thread using JNI (C++)

查看:255
本文介绍了故障使用JNI调用Java方法从本地线程(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,JNI,我希望有人能帮助我。结果
我试图从本地线程调用LUSOutputJNI一个Java类的构造函数中调用。结果
它使这个特定类的失败上的findClass(...)。

I have a JNI problem which I hope someone can help me out with.
I'm trying to call on a constructor of a Java class called LUSOutputJNI from a native thread.
It keeps failing on FindClass(...) of this specific class.

下面是code:

 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指针不再是空的结果。

  • 我肯定LUSOutputJNI的包名声明(三重检查吧...)

  • 当我尝试运行的findClass(..)有一种比较流行的类名如Android /空间/ TextView的,我得到一个积极的比赛。它的存在。这意味着线程附件和ENV变量都OK。 (我可以假设?)

  • 当我尝试从具有运行它JNI线程JNI方法运行以下code,它找到LUSOutputJNI类没有问题。

我是什么做错了吗?

帮助将非常AP preciated:)

Help will be much appreciated :)

感谢您的时间,

伊达

推荐答案

在这儿找到了答案\\工作。 (寻找常见问题解答:FindClass后面没有发现我的课,在
JNI提示

Found the answer \ work around in here. (Look for FAQ: "FindClass didn't find my class" in JNI tips)

我basicaly保存一个全球性的裁判所需的JCLASS对象。结果
然而,也必须克服一些邪恶的JNI为了使code编译C / JNI和C ++ / JNI之间变化。结果
这是我得到了NewGlobalRef编译和工作。

I basicaly saved a global ref to the needed jclass objects.
Did however had to overcome some evil JNI changes between C/JNI and C++/JNI in order for the code to compile.
This is how I got the NewGlobalRef to compile and work.

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 */
 }  

我希望这可以帮助任何人。

I hope this helps anyone.

这篇关于故障使用JNI调用Java方法从本地线程(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆