C++ 使用JNI(C+;+;)从本机线程调用Java方法时出现问题
我有一个JNI问题,希望有人能帮我解决。C++ 使用JNI(C+;+;)从本机线程调用Java方法时出现问题,c++,java-native-interface,android-ndk,C++,Java Native Interface,Android Ndk,我有一个JNI问题,希望有人能帮我解决。 我试图从本机线程调用名为LUSOutputJNI的Java类的构造函数。 它在这个特定类的FindClass(…)上不断失败 代码如下: LOGE("1"); JNIEnv *env = NULL; LOGE("2"); int res = -1; res = g_vm->AttachCurrentThread(&env, NULL); if(env == NULL) {
我试图从本机线程调用名为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");
观察结果:
- 我从AttachCurrentThread获得的结果为0,这意味着此附件成功+环境指针不再为NULL
- 我确信LUSOutputJNI的包名声明(经过三次检查…)
- 当我尝试使用更流行的类名(如android/widget/TextView)运行FindClass(..)时,我得到了一个肯定的匹配。它就在那里。这意味着线程附件和环境变量都正常。(我可以这样假设吗?)
- 当我尝试从一个JNI方法运行下面的代码时,该方法有一个运行它的JNI线程,它会毫无问题地找到LUSOutputJNI类李>
Ita要获得更多关于出错原因的信息(它将登录到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!!");
找到答案\在这里工作。(在中查找常见问题:“FindClass未找到我的类” ) 我基本上保存了对所需jclass对象的全局引用。
但是,Did必须克服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 */
}
我希望这对任何人都有帮助。有人引用过这个“LUSOutputJNI”类吗?@K-ballo:可能是内部类。@Ita:您的LUSOutputJNI静态初始化器是否引发异常?您应该打印引发的异常。@lta您应该修复错误处理。这让我哭了。LUSOutputJNI在类路径中吗?看起来您可以使用jthrowable中的toString函数获取错误信息,然后将其转换为C字符串,并将其放入
LOGE
函数中,但这可能是调试该函数的过度技巧。谢谢,这很有帮助。是否真的需要在此处删除本地引用?我相信当函数返回时,它们会自动删除:
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 */
}