我就是这样做的字符串:
void SampleSwitchCamera(const char *name)
{
jboolean isAttached;
JNIEnv *env;
jmethodID mid;
jstring js;
LOGE("SampleSwitchCamera Begin");
env = getJniEnv(&isAttached);
if (env == NULL)
goto FAIL0;
mid = getApplicationJniMethodId(env, applicationJniObj, "cameraSwitchCallback", "(Ljava/lang/String;)V");
if (mid == NULL)
goto FAIL1;
js = (*env)->NewStringUTF(env, name);
(*env)->CallVoidMethod(env, applicationJniObj, mid, js);
if (isAttached)
{
(*global_vm)->DetachCurrentThread(global_vm);
}
LOGE("SampleSwitchCamera End");
return;
FAIL1:
if (isAttached)
{
(*global_vm)->DetachCurrentThread(global_vm);
}
FAIL0:
LOGE("SampleSwitchCamera FAILED");
return;
}
所以我试过这样:
void CallbackFromJni(const int *id)
{
jboolean isAttached;
JNIEnv *env;
jmethodID mid;
jstring js;
LOGE("callbackFromJni Begin %d", id);
env = getJniEnv(&isAttached);
if (env == NULL)
goto FAIL0;
mid = getApplicationJniMethodId(env, applicationJniObj, "callbackFromJni", "(Ljava/lang/Integer;)V");
if (mid == NULL)
goto FAIL1;
jintArray array = (*env)->NewIntArray(env, id);
(*env)->CallVoidMethod(env, applicationJniObj, mid, array);
if (isAttached)
{
(*global_vm)->DetachCurrentThread(global_vm);
}
LOGE("callbackFromJni End");
return;
FAIL1:
if (isAttached)
{
(*global_vm)->DetachCurrentThread(global_vm);
}
FAIL0:
LOGE("callbackFromJni FAILED");
return;
}
但是我收到了这个错误:
01-26 11:17:36.654: E/VidyoMobile app/src/main/jni/ndkVidyoSample.c(22071): callbackFromJni Begin 101
01-26 11:17:36.655: E/art(22071): JNI ERROR (app bug): attempt to pass an instance of int[] as argument 1 to void com.vidyo.vidyocore.VidyoCoreApplication.callbackFromJni(java.lang.Integer)
01-26 11:17:36.692: A/art(22071): art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: bad arguments passed to void com.vidyo.vidyocore.VidyoCoreApplication.callbackFromJni(java.lang.Integer) (see above for details)
01-26 11:17:36.692: A/art(22071): art/runtime/java_vm_ext.cc:410] native: #09 pc 000024a9 /data/app/be.belfius.videocall-1/lib/arm/libndkVidyoSample.so (CallbackFromJni+104)
01-26 11:17:37.219: A/art(22071): art/runtime/runtime.cc:366] native: #12 pc 000024a9 /data/app/be.belfius.videocall-1/lib/arm/libndkVidyoSample.so (CallbackFromJni+104)
01-26 11:17:37.220: A/art(22071): art/runtime/runtime.cc:366] native: #14 pc 000024a9 /data/app/be.belfius.videocall-1/lib/arm/libndkVidyoSample.so (CallbackFromJni+104)
01-26 11:17:37.365: A/DEBUG(195): Abort message: 'art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: bad arguments passed to void com.vidyo.vidyocore.VidyoCoreApplication.callbackFromJni(java.lang.Integer) (see above for details)'
01-26 11:17:37.389: A/DEBUG(195): #14 pc 000024a9 /data/app/be.belfius.videocall-1/lib/arm/libndkVidyoSample.so (CallbackFromJni+104)
我是asumming因为我需要一个int,这是创建一个intArray。我怎么能创建一个int,因为没有newInt,只有newIntArray
答案 0 :(得分:1)
您声明获取 Integer
对象作为参数
mid = getApplicationJniMethodId(env,applicationJniObj,“callbackFromJni”,“( Ljava / lang / Integer; )V”);
但是您将整数表int[]
作为参数传递
jintArray array = (*env)->NewIntArray(env, id);
(*env)->CallVoidMethod(env, applicationJniObj, mid, array);
这里描述了声明要使用的类型的正确方法:https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html
如果要将Integer
作为参数传递,则需要将创建表更改为创建单个Integer对象
jclass int_cls = (*env)->FindClass(env, "java/lang/Integer");
if (int_cls == NULL)
goto FAIL1;
jmethodID int_constructor = (*env)->GetMethodID(env, int_cls, "<init>", "(I)V");
if (int_constructor == NULL)
goto FAIL1;
jobject java_int = (*env)->NewObject(env, int_cls, int_constructor, (jint)*id);
if (java_int == NULL)
goto FAIL1;
(*env)->CallVoidMethod(env, applicationJniObj, mid, java_int);
在这种情况下,Java端的方法需要看起来像:
native void callbackFromJni(Integer result);
而不是Integer,传递简单的int
而不是Integer
要容易得多,因为原始类型可以直接传递而不是将它们装入对象中
mid = getApplicationJniMethodId(env, applicationJniObj, "callbackFromJni", "(I)V");
if (mid == NULL)
goto FAIL1;
(*env)->CallVoidMethod(env, applicationJniObj, mid, *id);
在这种情况下,Java端的方法需要看起来像:
native void callbackFromJni(int result);
如果您尝试传递整数数组int[]
,则需要声明
mid = getApplicationJniMethodId(env, applicationJniObj, "callbackFromJni", "([I)V");
传递给创建新表的参数是大小,而不是数组本身,因此您需要先获取该数组的大小并在创建表时使用它
jintArray array = (*env)->NewIntArray(env, size);
然后设置数组内容并将其传递给回调
(*env)->SetIntArrayRegion(env, array, 0, size, id);
(*env)->CallVoidMethod(env, applicationJniObj, mid, array);
在这种情况下,Java端的方法需要看起来像:
native void callbackFromJni(int[] result);