请原谅我,因为我是 c++ jni 环境的新手
我正在尝试通过 JNI 桥将 Java 中的 String[] 数组传递给 c++,同时遵循我在此处找到的提示,给出以下代码段:
void MyJNIFunction(JNIEnv *env, jobject object, jobjectArray stringArray) {
int stringCount = env->GetArrayLength(stringArray);
for (int i=0; i<stringCount; i++) {
jstring string = (jstring) (env->GetObjectArrayElement(stringArray, i));
const char *rawString = env->GetStringUTFChars(string, 0);
// Don't forget to call `ReleaseStringUTFChars` when you're done.
}
}
我已经在我的代码中尝试了很多方法,但我仍然从 logcat 和我的应用程序停止中收到“无法修复的损坏”错误。请查看我的代码和帮助。非常感谢你。
我的代码:
JNIEXPORT void Java_ran_com_FirstActivity_Example(
JNIEnv *javaEnvironment, jobject self, jobjectArray stringArray, jlongArray params) {
// Convert the input jlong array to a regular int array.
jlong *longParams = javaEnvironment->GetLongArrayElements(params, JNI_FALSE);
int arr[6];
for (int n = 0; n < 6; n++) arr[n] = longParams[n];
javaEnvironment->ReleaseLongArrayElements(params, longParams, JNI_ABORT);
int stringCount = javaEnvironment->GetArrayLength(stringArray);
for (int i=0; i<stringCount; i++) {
jstring string = (jstring) (javaEnvironment->GetObjectArrayElement(stringArray, 2));
const char *rawString = javaEnvironment->GetStringUTFChars(string, 0);
// a method I created
example = new Example(rawString, arr);
javaEnvironment->ReleaseStringUTFChars(string, rawString);
}
}