我正在尝试在我的类中设置一个String Array字段,但遇到了各种各样的问题。这就是我到目前为止所做的:
JNIEXPORT jint JNICALL Java__Names1_GetMyNames(JNIEnv *env, jobject obj)
{
jfieldID myNamesID; /* store the field ID */
int i=0;
jstring myStr;
jobject dev;
char* myNames[] = {"Hello ", "world!"};
// Get a reference to obj’s class
jclass cls = env->GetObjectClass(obj);
// Get static field ID
myNamesID = env->GetStaticFieldID(cls, "myDevNames", "[Ljava/lang/String;");
// Get the object field
dev = env->GetObjectField(obj,myNamesID);
for (i=0; i<2; i++) {
myStr = env->NewStringUTF(myNames[i]);
env->SetObjectArrayElement(dev,i,myStr);
}
return 0;
}
这是我的字符串:
private static String[] myDevNames= new String[12];
当我尝试编译时,我收到以下错误:
错误:从'jobject {aka _jobject *}'无效转换为'jobjectArray {aka _jobjectArray *}'[-fpermissive]
任何想法我做错了什么?任何帮助将不胜感激。
答案 0 :(得分:3)
更新 - 我以前建议使用jarray而不是jobject是不正确的。我在看旧文档。基于http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html这个怎么样?将dev重新定义为jobjectArray,但您还需要对GetStaticObjectField的调用进行强制转换。
JNIEXPORT jint JNICALL Java__Names1_GetMyNames(JNIEnv *env, jobject obj)
{
jfieldID myNamesID; /* store the field ID */
int i=0;
jstring myStr;
jobjectArray dev;
char* myNames[] = {"Hello ", "world!"};
// Get a reference to obj’s class
jclass cls = env->GetObjectClass(obj);
// Get static field ID
myNamesID = env->GetStaticFieldID(cls, "myDevNames", "[Ljava/lang/String;");
// Get the object field
dev = static_cast<jobjectArray>(env->GetStaticObjectField(cls,myNamesID));
for (i=0; i<2; i++) {
myStr = env->NewStringUTF(myNames[i]);
env->SetObjectArrayElement(dev,i,myStr);
}
return 0;
}