我正在尝试将一个String []数组从java传递到JNI桥接到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.
}
}
我在我的代码中已经尝试了很多方法,但我仍然得到了一个不可挽回的&#34;#34;来自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);
}
}
答案 0 :(得分:1)
非常感谢timrau让我有信心深入了解我的“jni”代码。我最后通过将字符串提取到字符来解决这个问题,然后再将它们传递给我将使用它们的方法
JNIEXPORT void Java_ran_com_FirstActivity(JNIEnv *javaEnvironment, jobject self, jobjectArray stringArray) {
int stringCount = javaEnvironment->GetArrayLength(stringArray);
for (int i=0; i<stringCount; i++) {
jstring string = (jstring) (javaEnvironment->GetObjectArrayElement(stringArray, 0));
const char *rawString = javaEnvironment->GetStringUTFChars(string, JNI_FALSE);
jstring istring = (jstring) (javaEnvironment->GetObjectArrayElement(stringArray, 1));
const char *rawStringb = javaEnvironment->GetStringUTFChars(istring, JNI_FALSE);
// I extracyed both string items to different chars before passing //to my methods...since jni only support passing two custom parameters
example = new SuperpoweredExample(rawString, rawStringb);
javaEnvironment->ReleaseStringUTFChars(string, rawString);
javaEnvironment->ReleaseStringUTFChars(istring, rawStringb);
}
}
答案 1 :(得分:0)
int stringCount = javaEnvironment->GetArrayLength(stringArray);
for (int i=0; i<stringCount; i++) {
jstring string = (jstring) (javaEnvironment->GetObjectArrayElement(stringArray, 2));
您可能确实打算调用以下行来枚举stringArray
中的所有字符串。
jstring string = (jstring)(javaEnvironment->GetObjectArrayElement(stringArray, i);
//i, not 2