解决:我需要在调用方法之前创建一个新的对象!JNI方法不回调Java方法
我一直在试图从JNI调用Java方法。该调用起作用,应用程序不会崩溃,但JAVA方法也不会被调用。
任何想法,我做错了什么?
我的JNI代码:
jclass test_method = findClass("com/package/Test");
if (test_method == NULL) {
__android_log_print(ANDROID_LOG_ERROR, "JNI", "failed to find test");
} else {
__android_log_print(ANDROID_LOG_ERROR, "JNI", "found test object");
}
jmethodID test_const = env->GetMethodID(test_method, "<init>", "(I)V");
jobject employeeObject = (env)->NewObject(test_method, test_const, 800);
if (employeeObject == NULL) {
__android_log_print(ANDROID_LOG_ERROR, "JNI", "failed to find object");
} else {
__android_log_print(ANDROID_LOG_ERROR, "JNI", "found emp object");
}
jclass myclass = findClass("com/package/TestObj");
if(myclass == NULL) {
__android_log_print(ANDROID_LOG_ERROR, "JNI", "Couldnt find myclass");
return;
} else {
__android_log_print(ANDROID_LOG_ERROR, "JNI", "Found myclass");
}
g_clazz = (jclass)env->NewGlobalRef(myclass);
if (g_clazz == NULL) {
fLogPrintf(LOG_DEBUG,"Failed to find class");
}
new_method = env->GetMethodID(g_clazz, "setParams", "(Lcom/package/Test;)V");
if (new_method == 0) {
__android_log_print(ANDROID_LOG_ERROR, "JNI", "failed to find method");
}
(*env).CallVoidMethod(g_clazz, new_method, employeeObject);
__android_log_print(ANDROID_LOG_ERROR, "JNI", "succesfully finished calling method");
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
}
__android_log_print(ANDROID_LOG_ERROR, "JNI", "Detaching current thread");
m_jvm->DetachCurrentThread();
我的Java代码:
package com.package;
class TestObj {
public void setParams(Test test) {
Log.e("Oops", " Callback successfully called!");
}
}
当我运行它运行完全没有任何错误/崩溃的应用程序,我也得到了“成功完成调用方法”和“分离当前线程”日志,表明JNI能够成功调用我的函数,但是当我检查Java方法setParams的日志时,我根本看不到日志。
任何想法我错过了什么?
感谢
new_method = env-> GetMethodID(g_clazz,“setParam s“,”(Lcom/package/Test;)V“);我认为你在这一行的论点是错误的,应该测试TestSIP? – 2014-11-03 12:59:17
哎呀这是一个错字,它应该是测试。更新了问题。另外,我不认为命名是问题,因为它不会崩溃,如果命名错误,它通常会发生。我得到所有的日志结尾 – 2014-11-03 13:01:40
env-> GetMethodID不返回一个对象,但0如果方法没有找到,你应该检查。 NULL检查不起作用,因为它从不返回NULL。 – 2014-11-03 13:03:28