简体   繁体   中英

JNI Error accessed stale weak global reference

I am trying to call NfcAdapter.setNdefPushMessageCallback from JNI layer. As you must be aware the signature for the same is 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; (va_list), I am not giving any other argument.

In case of Java this is perfectly accepted :

NfcAdapter.setNdefPushMessageCallback(callback, activity);

Please suggest me the solution

This method signature requires at least one Activity as arguments.
If you don't provide anything for the last activities argument, Java will automatically create a new empty array.
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 ).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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