Ita*_*Ita 5 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)
{
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");
Run Code Online (Sandbox Code Playgroud)
这是失败时的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
Run Code Online (Sandbox Code Playgroud)
观察:
我究竟做错了什么?
帮助将不胜感激:)
谢谢你的时间,
伊达
找到答案\在这里工作.(寻找常见问题解答:"FindClass在JNI提示中找不到我的课程" )
我基本上保存了一个全局引用到所需的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 */
}
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助任何人.
| 归档时间: |
|
| 查看次数: |
7591 次 |
| 最近记录: |