gpt4 book ai didi

android - 如何使用 getApplicationJniMethodId 发送一个 int 作为参数?

转载 作者:行者123 更新时间:2023-11-28 05:19:12 25 4
gpt4 key购买 nike

这是我为 String 做的:

 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)

我正在假设,因为我需要一个 int,而这正在创建一个 intArray。我如何创建一个 int,因为没有 newInt,只有 newIntArray

最佳答案

你声明了获取Integer对象作为参数的get方法

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);

传递简单的 int 而不是 Integer 比 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);

关于android - 如何使用 getApplicationJniMethodId 发送一个 int 作为参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41871300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com