正确的 JNI 转换从 jobjectarray 获取数组项到单独的 const char
Proper JNI conversion getting array items from jobjectarray to seperate const char
请原谅我,因为我是 c++ jni 环境的新手
我正在尝试通过 JNI 桥将一个 String[] 数组从 java 传递到 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 的 "broken irreparably" 错误并且我的应用程序停止了。请看看我的代码和帮助。非常感谢您。
我的代码:
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);
}
}
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
非常感谢 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);
}
}
请原谅我,因为我是 c++ jni 环境的新手
我正在尝试通过 JNI 桥将一个 String[] 数组从 java 传递到 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 的 "broken irreparably" 错误并且我的应用程序停止了。请看看我的代码和帮助。非常感谢您。
我的代码:
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);
}
}
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
非常感谢 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);
}
}