[英]Passing an object in JNI always returns NULL
我正在使用JNI将对象从C ++传递给Java。 但是被卡在代码中的某个位置,其中“ GetMethodID”始终返回NULL并崩溃了! 以下是我使用的头文件以及其后的cpp代码。
我的意图是从JNI返回一个对象,在下面的cpp代码中,我总是在其中得到“ emu_response_constructor Null”。
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_em_Grabber */
#ifndef _Included_com_em_Grabber
#define _Included_com_em_Grabber
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_em_Grabber
* Method: getProcessedImage
* Signature: (II[B)Lcom/em/Response;
*/
JNIEXPORT jobject JNICALL Java_com_em_Grabber_getProcessedImage
(JNIEnv *, jobject, jint, jint, jbyteArray);
#ifdef __cplusplus
}
#endif
#endif
以下是我使用的cpp文件。 对象传递到Java的位置。
JNIEXPORT jobject JNICALL Java_com_em_Grabber_getProcessedImage
( JNIEnv *env, jobject obj, jint jRows, jint jCols,jbyteArray jByteArray ){
jclass emu_response = env->FindClass("com/em/Response");
if (emu_response == NULL) {
cout << "emu_response Null" << endl;
}
jmethodID emu_response_constructor = env -> GetMethodID(emu_response, "<init>", "(II[B)Lcom/em/Response;");
if (NULL == emu_response_constructor ) {
cout << "emu_response_constructor Null" << endl;
}
int number = 90;
jobject jEmuResponse = env->NewObject ( emu_response,emu_response_constructor, jByteArray, number );
return jEmuResponse;
}
下面是我的java类:
public class Response {
private int age;
private byte[] result = null;
public Response()
{
}
public Response(byte[] result,int age) {
this.age = age;
this.result = result;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public byte[] getResult() {
return result;
}
public void setResult(byte[] result) {
this.result = result;
}
不要试图自己组成JNI签名。 使用javap -s的输出。
在这种情况下,很明显您的参数类型混乱。 我认为正确的签名是“((BI)Lcom / em / Response;”),但请不要相信我的意思:使用javap -s。 总是。
并修复您的代码,这样,如果您从任何API中获得了意外结果,您就不会只是打印并继续,就好像值合法。 这就是导致崩溃的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.