public class Student
{
private People people;
private Result result;
private int amount;
}
这是 Java 类的示例;在C中,我试图获取“Student”中的“people”,但我失败了。但是,我能够从“Student”获取 int 类型的“amount”。
jobject getObjectFromObject(JNIEnv *env, jobject obj, const char * fieldName)
{
jfieldID fid; /* store the field ID */
jobject i;
/* Get a reference to obj's class */
jclass cls = (*env)->GetObjectClass(env, obj);
/* Look for the instance field s in cls */
fid = (*env)->GetFieldID(env, cls, fieldName, "L");
if (fid == NULL)
{
return 0; /* failed to find the field */
}
/* Read the instance field s */
i = (*env)->GetObjectField(env, obj, fid);
return i;
}
我试图将“people”作为 fieldName 传递给方法,但它仍然给出以下错误:“java.lang.NoSuchFieldError: people”
最佳答案
记录在案here ,在 GetFieldID
方法中不能单独使用“L”作为类型签名,必须在其后指定类名。
例如,如果你想指定参数是一个String
,你将不得不使用Ljava/lang/String;
(最后的分号是签名!)。
对于名为 People
的自定义类,假设它在包 your.package.name
中,则必须使用 Lyour/package/name/人;
作为类型签名。
关于java - 在 C 中使用 JNI 从对象获取对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15783344/