无法初始化类型' jstring'的变量(又名' _jstring *'),其左值为' jobject' (又名' _jobject *')

时间:2017-08-05 17:34:39

标签: android android-ndk

有谁知道如何解决错误?

JNIEXPORT jstring JNICALL JAVA_com_pfc_camera_ndkmain_MainActivity_compresion(JNIEnv* env, jobject obj, jobjectArray jargv){



//jargv is a Java array of Java strings
    int argc = env->GetArrayLength(jargv);
    typedef char *pchar;
    pchar *argv = new pchar[argc];
    int i;
    for(i=0; i<argc; i++)
    {
        jstring js = env->GetObjectArrayElement(jargv, i); //A Java string
        const char *pjc = env->GetStringUTFChars(js); //A pointer to a Java-managed char buffer
        size_t jslen = strlen(pjc);
        argv[i] = new char[jslen+1]; //Extra char for the terminating null
        strcpy(argv[i], pjc); //Copy to *our* buffer. We could omit that, but IMHO this is cleaner. Also, const correctness.
        env->ReleaseStringUTFChars(js, pjc);
    }

    //Call main
    Principal *pa=Principal::CreateInstance(argc,argv);
    pa->Run();
    pa->FreeInstance();

    //Now free the array
    for(i=0;i<argc;i++)
        delete [] argv[i];
    delete [] argv;

}

我知道错误可能来自不进行投射,但我没有非常清楚

[https://i.stack.imgur.com/bOWKZ.png][1]

似乎问题已经解决了,现在我又得到了另一个错误,但我不明白我是否传递了两个参数js和pjc

[https://i.stack.imgur.com/UHCAR.png][1]

1 个答案:

答案 0 :(得分:1)

在C ++中,您必须使用显式转换为您想要的类型。

jstring js = (jstring)env->GetObjectArrayElement(jargv, i);

您可以了解jni编程here

关于您的其他问题,请执行以下操作:

const jbyte *pjc = env->GetStringUTFChars(js, NULL);