我试图在我的课程中设置一个字符串数组字段,但遇到各种问题。这是我到目前为止所拥有的:
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]
任何想法我在做什么错?任何帮助,将不胜感激。
最佳答案
更新-我以前使用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;
}