我正在尝试使用 Android Studio1.3.2 + NDK + Windows7 构建程序。 我想将一个字符串从 Java 函数传递给 native C 函数 (const char*)。
native C 函数声明是:-
public native int HRFromJNI(String path);
Java 函数调用是:-
tv.setText(String.valueOf(HRFromJNI(path)));
Native C 函数是:-
extern "C" {
JNIEXPORT jint JNICALL
Java_com_example_hellojni_HelloJni_HRFromJNI
(JNIEnv *env, jobject obj,jstring path)
{
int HRval = 0;
char *Path;
Path = (*env)->GetStringUTFChars( env, path, null) ;
HRval = filefunction(Path);
return HRval;
}
Native C函数调用的函数是:-
int filefunction(char* filename)
{
FILE* file = fopen((char*)filename,"w+");
//Reads value from the file and returns it.
.
.
.
}
但我收到错误“-> 的基操作数具有非指针类型 JNIEnv{aka _JNIEnv}”
这是将字符串传递给 native C 代码的正确方法,还是有任何其他方法可以将字符串从 Java 分配给 const char* ?
最佳答案
更改以下行:
Path = (*env)->GetStringUTFChars( env, path, null) ;
收件人:
Path = env->GetStringUTFChars( env, path, JNI_TRUE) ;
传递 jni boolean 变量,而不是传递 null。
关于java - 将 java 字符串处理为 Android Studio+NDK 的原生 C/C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34851470/