Error calling Java method from native thread using JNI (C ++)

I have a problem with JNI and I hope someone can help me. I am trying to call the constructor of the Java class LUSOutputJNI from a native thread.
It continues to fail in FindClass (...) of this particular class.

Here is the 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");    

Here is the output of logcat when it fails:

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  

Observations:

  • I get the result of AttachCurrentThread, which is 0, which means that this attachment was successful + the env pointer is no longer NULL.
  • I am confident in declaring the package name LUSOutputJNI (triple checked it ...)
  • FindClass (..) , android/widget/TextView, . . , env . ( , ?)
  • JNI-, JNI, LUSOutputJNI .

?

:)

,

​​

+5
2

\ . ( FAQ: "FindClass " JNI)

basicaly ref jclass.
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 */
 }  

, .

+6

, ( 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!!");
+2

All Articles