简体   繁体   English

JNI错误访问过时的弱全局引用

[英]JNI Error accessed stale weak global reference

I am trying to call NfcAdapter.setNdefPushMessageCallback from JNI layer. 我正在尝试从JNI层调用NfcAdapter.setNdefPushMessageCallback。 As you must be aware the signature for the same is setNdefPushMessageCallback(NfcAdapter.CreateNdefMessageCallback callback, Activity activity, Activity... activities) 您必须知道,相同的签名是setNdefPushMessageCallback(NfcAdapter.CreateNdefMessageCallback callback, Activity activity, Activity... activities)

I have gone through some references to this problem one here and another one 我已经在这里另一篇文章中对这个问题进行了一些参考

I have taken care of all those suggestions. 我已经考虑了所有这些建议。 I suspect it must be related to second one. 我怀疑它一定与第二个有关。

Here is my code : 这是我的代码:

    jmethodID methodId = (*env)->GetMethodID(
    env, cls, "setNdefPushMessageCallback",
    "(Landroid/nfc/NfcAdapter$CreateNdefMessageCallback;Landroid/app/Activity;[Landroid/app/Activity;)V");

(*env)->CallVoidMethod(env, g_adapter, methodId, g_nfcCallback, g_activity);

In the place of [Landroid/app/Activity; 代替[Landroid / app / Activity; (va_list), I am not giving any other argument. (va_list),我没有提出任何其他论点。

In case of Java this is perfectly accepted : 如果是Java,则完全可以接受:

NfcAdapter.setNdefPushMessageCallback(callback, activity);

Please suggest me the solution 请给我建议解决方案

This method signature requires at least one Activity as arguments. 此方法签名至少需要一个Activity作为参数。
If you don't provide anything for the last activities argument, Java will automatically create a new empty array. 如果您没有为最后一个activities参数提供任何信息,Java将自动创建一个新的空数组。
Unfortunately, the JNI layer won't do it automatically and the corresponding code will crash if it receive a null argument ( the foreach loop here ). 不幸的是,JNI层不会自动执行该操作,并且如果接收到空参数( 此处为foreach循环 ),则相应的代码将崩溃。

Therefore, you need to pass an empty array to the method call: 因此,您需要将一个空数组传递给方法调用:

jobjectArray empty = (jobjectArray) (*env)->NewObjectArray(env, 0, (*env)->FindClass(env, "Landroid/app/Activity;"), NULL);
(*env)->CallVoidMethod(env, g_adapter, methodId, g_nfcCallback, g_activity, empty);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM