JNI - GetObjectField在类内部类失败

时间:2015-08-09 08:12:15

标签: java android java-native-interface

我正在尝试访问Java对象中的Java对象。我查看了Accessing a Java object in a Java object in C using JNI,但我的应用仍然在GetObjectField中出错。

错误是:

sart/tuntime/check_jni:.cc65] JNI DETECTED ERROR IN APPLICATION: 
jfieldID my.package.name.SubObject my.package.name.MainObject.subObject
not valid for an object of class java.lang.Class<my.package.name.MainObject>

我的代码是:

MainObject

package my.package.name;
public class MainObject {
    SubObject subObject= new SubObject();
}

子对象

package my.package.name;
public class SubObject {
    String name;
}

JNI

void ReadJavaObject(JNIEnv * env,  jobject mainObjectJava)
{
    jclass cls = env->GetObjectClass(mainObjectJava);   
    jfieldID subObjectID = env->GetFieldID(cls, "subObject", "Lmy/package/name/SubObject;");
    jobject subObjectJava= env->GetObjectField(cls, subObjectID);
//...Do something with subObjectJava
}

1 个答案:

答案 0 :(得分:1)

似乎你只是在最后一次通话中使用了错误的参数。 GetObjectField必须与根对象和objectId一起使用才能获得。

void ReadJavaObject(JNIEnv * env,  jobject mainObjectJava)
{
    jclass cls = env->GetObjectClass(mainObjectJava);   
    jfieldID subObjectID = env->GetFieldID(cls, "subObject", "Lmy/package/name/SubObject;");
    jobject subObjectJava= env->GetObjectField(mainObjectJava, subObjectID);
}