简体   繁体   中英

Error:(289, 23)C++ / JNI / NDK - error: cannot initialize a parameter of type 'char *' with an lvalue of type 'jstring' (aka '_jstring *')

I have an error working with C++/Android (JNI) and I absolutely don't know how to fix it.. (I'm very new to C++)

Error:(289, 23) error: cannot initialize a parameter of type 'char *' with an lvalue of type 'jstring' (aka '_jstring *')

the error points at this line

JNIEXPORT void Java_de_meetspot_ndktest_MainActivity_LoadPlayerA(JNIEnv *javaEnvironment, jobject self, jstring audioPath, jlongArray offsetAndLength) {
    example->LoadPlayerA(audioPath, offsetAndLength);
}

in my class-declaration I have:

public: void LoadPlayerA(char *audioPath, int *params);android

Can someone tell me, where I it wrong?

The error is pretty self explanatory. In example->LoadPlayerA(audioPath, offsetAndLength); the type of audioPath is a jstring but in your LoadPlayerA() function the first parameter is char *audioPath . The compiler does not know how to convert a jstring to a char* so you are going to have to do it yourself.

Borrowing from Jason Rogers answer here you can change your code to:

JNIEXPORT void Java_de_meetspot_ndktest_MainActivity_LoadPlayerA(JNIEnv *javaEnvironment, jobject self, jstring audioPath, jlongArray offsetAndLength)
{
    const char* audio = javaEnvironment->GetStringUTFChars(audioPath, JNI_FALSE)
    example->LoadPlayerA(audio, offsetAndLength);
    javaEnvironment->ReleaseStringUTFChars(audioPath, audio);
}

EDIT:

I forgot to include the Release() function. This needs to be called when you are done with the array otherwise you will have a memory leak.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM