Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JNI:对用户定义的类对象使用CallVoidMethod() 我在java中定义了一个类,称为MyList.我想创建并作为C++的一个参数传递给java方法。我已经成功地创建了对象并调用了回调方法,但我从未看到在Java中调用回调。没有错误或例外: jobject obj; jclass lClass; // Get constructor method for MyList Class mMyWatcher.MethodConst = mMyWatcher.mThreadEnv->GetMethodID(mMyWatcher.ClassMyList, "<init>", "()V"); if (mMyWatcher.MethodConst == NULL) { LOGE("Could not find constructor method from class"); } // Create new Java object of class MyList (call constructor basically) obj = mMyWatcher.mThreadEnv->NewObject(mMyWatcher.ClassMyList, mMyWatcher.MethodConst); if (mMyWatcher.MethodConst == NULL) { LOGE("Could not create object from class & constructor"); } // Get the MyList class for the new object lClass = mMyWatcher.mThreadEnv->GetObjectClass(obj); if (lClass == NULL) { LOGE("New MyList class is NULL"); } // Get the methods of the new class LOGI("Getting setName()"); mMyWatcher.MethodSetName = mMyWatcher.mThreadEnv->GetMethodID(lClass, "setName", "(Ljava/lang/String;)V"); if (mMyWatcher.MethodSetName == 0) { LOGE("Unable to find MyList constructor method"); } mMyWatcher.MethodSetIndex = mMyWatcher.mThreadEnv->GetMethodID(lClass, "setIndex", "(I)V"); if (mMyWatcher.MethodSetIndex == 0) { LOGE("Unable to find MyList setIndex() method"); //goto ERROR; } LOGI("Getting setMin()"); mMyWatcher.MethodSetMin = mMyWatcher.mThreadEnv->GetMethodID(lClass, "setMin", "(II)V"); if (mMyWatcher.MethodSetMin == 0) { LOGE("Unable to find MyList constructor method"); } LOGI("Getting setMax()"); mMyWatcher.MethodSetMax = mMyWatcher.mThreadEnv->GetMethodID(lClass, "setMax", "(II)V"); if (mMyWatcher.MethodSetMax == 0) { LOGE("Unable to find MyList constructor method"); } // Call the methods to set parameters jstring lValue = mMyWatcher.mThreadEnv->NewStringUTF("My_PARAM_1"); mMyWatcher.mThreadEnv->CallVoidMethod(obj, mMyWatcher.MethodSetName, lValue); mMyWatcher.mThreadEnv->DeleteLocalRef(lValue); mMyWatcher.mThreadEnv->CallVoidMethod(obj, mMyWatcher.MethodSetIndex, 0); mMyWatcher.mThreadEnv->CallVoidMethod(obj, mMyWatcher.MethodSetMin, 0, 0); mMyWatcher.mThreadEnv->CallVoidMethod(obj, mMyWatcher.MethodSetMax, 0, 0); lValue = mMyWatcher.mThreadEnv->NewStringUTF("Parm_800"); mMyWatcher.mThreadEnv->CallVoidMethod(obj, mMyWatcher.MethodAdd, 800, lValue); mMyWatcher.mThreadEnv->DeleteLocalRef(lValue); //This method never gets called in the java code, but there is no error mMyWatcher.mThreadEnv->CallVoidMethod(mMyWatcher.mObject, mMyWatcher.MethodOnListUpdate2, obj);_Java_Android_C++_Android Ndk_Java Native Interface - Fatal编程技术网

JNI:对用户定义的类对象使用CallVoidMethod() 我在java中定义了一个类,称为MyList.我想创建并作为C++的一个参数传递给java方法。我已经成功地创建了对象并调用了回调方法,但我从未看到在Java中调用回调。没有错误或例外: jobject obj; jclass lClass; // Get constructor method for MyList Class mMyWatcher.MethodConst = mMyWatcher.mThreadEnv->GetMethodID(mMyWatcher.ClassMyList, "<init>", "()V"); if (mMyWatcher.MethodConst == NULL) { LOGE("Could not find constructor method from class"); } // Create new Java object of class MyList (call constructor basically) obj = mMyWatcher.mThreadEnv->NewObject(mMyWatcher.ClassMyList, mMyWatcher.MethodConst); if (mMyWatcher.MethodConst == NULL) { LOGE("Could not create object from class & constructor"); } // Get the MyList class for the new object lClass = mMyWatcher.mThreadEnv->GetObjectClass(obj); if (lClass == NULL) { LOGE("New MyList class is NULL"); } // Get the methods of the new class LOGI("Getting setName()"); mMyWatcher.MethodSetName = mMyWatcher.mThreadEnv->GetMethodID(lClass, "setName", "(Ljava/lang/String;)V"); if (mMyWatcher.MethodSetName == 0) { LOGE("Unable to find MyList constructor method"); } mMyWatcher.MethodSetIndex = mMyWatcher.mThreadEnv->GetMethodID(lClass, "setIndex", "(I)V"); if (mMyWatcher.MethodSetIndex == 0) { LOGE("Unable to find MyList setIndex() method"); //goto ERROR; } LOGI("Getting setMin()"); mMyWatcher.MethodSetMin = mMyWatcher.mThreadEnv->GetMethodID(lClass, "setMin", "(II)V"); if (mMyWatcher.MethodSetMin == 0) { LOGE("Unable to find MyList constructor method"); } LOGI("Getting setMax()"); mMyWatcher.MethodSetMax = mMyWatcher.mThreadEnv->GetMethodID(lClass, "setMax", "(II)V"); if (mMyWatcher.MethodSetMax == 0) { LOGE("Unable to find MyList constructor method"); } // Call the methods to set parameters jstring lValue = mMyWatcher.mThreadEnv->NewStringUTF("My_PARAM_1"); mMyWatcher.mThreadEnv->CallVoidMethod(obj, mMyWatcher.MethodSetName, lValue); mMyWatcher.mThreadEnv->DeleteLocalRef(lValue); mMyWatcher.mThreadEnv->CallVoidMethod(obj, mMyWatcher.MethodSetIndex, 0); mMyWatcher.mThreadEnv->CallVoidMethod(obj, mMyWatcher.MethodSetMin, 0, 0); mMyWatcher.mThreadEnv->CallVoidMethod(obj, mMyWatcher.MethodSetMax, 0, 0); lValue = mMyWatcher.mThreadEnv->NewStringUTF("Parm_800"); mMyWatcher.mThreadEnv->CallVoidMethod(obj, mMyWatcher.MethodAdd, 800, lValue); mMyWatcher.mThreadEnv->DeleteLocalRef(lValue); //This method never gets called in the java code, but there is no error mMyWatcher.mThreadEnv->CallVoidMethod(mMyWatcher.mObject, mMyWatcher.MethodOnListUpdate2, obj);

JNI:对用户定义的类对象使用CallVoidMethod() 我在java中定义了一个类,称为MyList.我想创建并作为C++的一个参数传递给java方法。我已经成功地创建了对象并调用了回调方法,但我从未看到在Java中调用回调。没有错误或例外: jobject obj; jclass lClass; // Get constructor method for MyList Class mMyWatcher.MethodConst = mMyWatcher.mThreadEnv->GetMethodID(mMyWatcher.ClassMyList, "<init>", "()V"); if (mMyWatcher.MethodConst == NULL) { LOGE("Could not find constructor method from class"); } // Create new Java object of class MyList (call constructor basically) obj = mMyWatcher.mThreadEnv->NewObject(mMyWatcher.ClassMyList, mMyWatcher.MethodConst); if (mMyWatcher.MethodConst == NULL) { LOGE("Could not create object from class & constructor"); } // Get the MyList class for the new object lClass = mMyWatcher.mThreadEnv->GetObjectClass(obj); if (lClass == NULL) { LOGE("New MyList class is NULL"); } // Get the methods of the new class LOGI("Getting setName()"); mMyWatcher.MethodSetName = mMyWatcher.mThreadEnv->GetMethodID(lClass, "setName", "(Ljava/lang/String;)V"); if (mMyWatcher.MethodSetName == 0) { LOGE("Unable to find MyList constructor method"); } mMyWatcher.MethodSetIndex = mMyWatcher.mThreadEnv->GetMethodID(lClass, "setIndex", "(I)V"); if (mMyWatcher.MethodSetIndex == 0) { LOGE("Unable to find MyList setIndex() method"); //goto ERROR; } LOGI("Getting setMin()"); mMyWatcher.MethodSetMin = mMyWatcher.mThreadEnv->GetMethodID(lClass, "setMin", "(II)V"); if (mMyWatcher.MethodSetMin == 0) { LOGE("Unable to find MyList constructor method"); } LOGI("Getting setMax()"); mMyWatcher.MethodSetMax = mMyWatcher.mThreadEnv->GetMethodID(lClass, "setMax", "(II)V"); if (mMyWatcher.MethodSetMax == 0) { LOGE("Unable to find MyList constructor method"); } // Call the methods to set parameters jstring lValue = mMyWatcher.mThreadEnv->NewStringUTF("My_PARAM_1"); mMyWatcher.mThreadEnv->CallVoidMethod(obj, mMyWatcher.MethodSetName, lValue); mMyWatcher.mThreadEnv->DeleteLocalRef(lValue); mMyWatcher.mThreadEnv->CallVoidMethod(obj, mMyWatcher.MethodSetIndex, 0); mMyWatcher.mThreadEnv->CallVoidMethod(obj, mMyWatcher.MethodSetMin, 0, 0); mMyWatcher.mThreadEnv->CallVoidMethod(obj, mMyWatcher.MethodSetMax, 0, 0); lValue = mMyWatcher.mThreadEnv->NewStringUTF("Parm_800"); mMyWatcher.mThreadEnv->CallVoidMethod(obj, mMyWatcher.MethodAdd, 800, lValue); mMyWatcher.mThreadEnv->DeleteLocalRef(lValue); //This method never gets called in the java code, but there is no error mMyWatcher.mThreadEnv->CallVoidMethod(mMyWatcher.mObject, mMyWatcher.MethodOnListUpdate2, obj);,java,android,c++,android-ndk,java-native-interface,Java,Android,C++,Android Ndk,Java Native Interface,上面的代码没有问题,找到了方法。您也可以发布java类代码吗?一般建议:确保启用了CheckJNI。有时,它会发现一些导致奇怪行为的微妙问题。你的错误处理到了极点。如果你犯了这些错误,你就不能像没有犯一样继续下去。此外,您正在测试mMyWatcher.MethodConst==NULL,您应该在其中测试obj==NULL。如果出现任何错误,您应该调用其中一个异常检查方法来打印堆栈跟踪,这样您就知道它到底是什么:您应该在这里发布堆栈跟踪。除非使用全局引用或弱引用,否则无法缓存作业对象或jclass

上面的代码没有问题,找到了方法。

您也可以发布java类代码吗?一般建议:确保启用了CheckJNI。有时,它会发现一些导致奇怪行为的微妙问题。你的错误处理到了极点。如果你犯了这些错误,你就不能像没有犯一样继续下去。此外,您正在测试mMyWatcher.MethodConst==NULL,您应该在其中测试obj==NULL。如果出现任何错误,您应该调用其中一个异常检查方法来打印堆栈跟踪,这样您就知道它到底是什么:您应该在这里发布堆栈跟踪。除非使用全局引用或弱引用,否则无法缓存作业对象或jclass。
mMyWatcher.MethodOnListUpdate2 = pEnv->GetMethodID(mMyWatcher.ClassMyObject, "onListUpdate2", "(Lcom/example/test/MyList;)V");
if (mMyWatcher.MethodOnListUpdate2 == NULL) {
    LOGE("JNI: Error caching the methods: onListUpdate2()");
    goto ERROR;
}