0

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

Community
  • 1
  • 1
BlackPearl
  • 31
  • 4
  • Try to use a `NULL` reference for the expected vararg. – Simon Marquis Aug 26 '15 at 19:45
  • I have tried this already. I get the below error `08-28 09:19:46.161: E/AndroidRuntime(22601): Process: org.iotivity.ca.sample_service, PID: 22601 08-28 09:19:46.161: E/AndroidRuntime(22601): java.lang.NullPointerException: Attempt to get length of null array 08-28 09:19:46.161: E/AndroidRuntime(22601): at android.nfc.NfcAdapter.setNdefPushMessageCallback(NfcAdapter.java:1279)` – BlackPearl Aug 28 '15 at 00:21

1 Answers1

0

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);
Simon Marquis
  • 7,248
  • 1
  • 28
  • 43
  • Thanks a lot Marquis. Finally this has resolved the problem and seems to be the best way to do it. I still have a question on mind, that Does JNI allow to `NewObjectArray` with length 0? I have a small correction to the implementation that you gave : `jobjectArray tempArr = (jobjectArray) (*env)->NewObjectArray(env, 0, (*env)->FindClass(env, "android/app/Activity"), NULL);` – BlackPearl Aug 31 '15 at 11:28