Android Question [SOLVED] Error: Array has incompatible type class...

DavideV

Active Member
Licensed User
Longtime User
Hi developers,
I'm getting crazy with an error that crashes my app when I try to write NDEF records to an NFC Tag.

It looks like I'm passing wrong array type, but I can't understand:

Here is a working code, used until now, NDEFRecords are hardcoded (R0,R1,R2...):
B4X:
'connect to the tag, entry point for reading and writing!
Private Sub Ndef_Connected (Success As Boolean)
    'Log($"Ndef reader/writer connected: ${Success}"$) 
    If Success = False Then
        NFCTagWriter_Error (Starter.mloc.Localize("actwritetag_error_tag_not_connected"))
    Else
        If isWriting Then
            #if dev
            LogColor("ActWriteTag>Writing mode",Colors.green)
            #End If
 

            Dim R0  As NdefRecord=NativeMe.RunMethod("createTextRecord",Array As Object("en",TagText(0)))
            Dim R1  As NdefRecord=NativeMe.RunMethod("createTextRecord",Array As Object("en",TagText(1)))
            Dim R2  As NdefRecord=NativeMe.RunMethod("createTextRecord",Array As Object("en",TagText(2)))
            Dim R3  As NdefRecord=NativeMe.RunMethod("createTextRecord",Array As Object("en",TagText(3)))
            Dim R4  As NdefRecord=NativeMe.RunMethod("createTextRecord",Array As Object("en",TagText(4)))
            Dim R5  As NdefRecord=NativeMe.RunMethod("createTextRecord",Array As Object("en",TagText(5)))
            Dim R6  As NdefRecord=NativeMe.RunMethod("createTextRecord",Array As Object("en",TagText(6)))
            Dim R7  As NdefRecord=NativeMe.RunMethod("createTextRecord",Array As Object("en",TagText(7)))
            Dim R8  As NdefRecord=NativeMe.RunMethod("createTextRecord",Array As Object("en",TagText(8)))
            Dim R9  As NdefRecord=NativeMe.RunMethod("createTextRecord",Array As Object("en",TagText(9)))
            WriteNdef(Array(R0,R1,R2,R3,R4,R5,R6,R7,R8,R9))
       
      

  
    End If
end sub

'write the records already created to the tag
Private Sub WriteNdef (Records() As Object)
    Dim RecordsJO As JavaObject
    RecordsJO.InitializeArray("android.nfc.NdefRecord", Records)
    Dim message As JavaObject
    message.InitializeNewInstance("android.nfc.NdefMessage", Array(RecordsJO))
    'Write!
    TagTech.RunAsync("WriteNdef", "writeNdefMessage", Array(message), 0)
 
End Sub


The code below does not work, but I need to use it in some way because I don't know the number of records in advance so they cannot be hardcoded...:

B4X:
'connect to the tag, entry point for reading and writing!
Private Sub Ndef_Connected (Success As Boolean)
    'Log($"Ndef reader/writer connected: ${Success}"$) 
    If Success = False Then
        NFCTagWriter_Error (Starter.mloc.Localize("actwritetag_error_tag_not_connected"))
    Else
        If isWriting Then
      

            'new V1.15
            Dim recs(tagList.Size) As NdefRecord
            For x=0 To tagList.Size-1
                Dim str As String=tagList.get(x)
                Log("Writing record: " & str)
                recs(x)=NativeMe.RunMethod("createTextRecord",Array As Object("en",str))
            Next
            WriteNdef(recs)

    End If

end sub

'write the records already created to the tag
Private Sub WriteNdef (Records() As Object)
    Dim RecordsJO As JavaObject
    RecordsJO.InitializeArray("android.nfc.NdefRecord", Records)
    Dim message As JavaObject
    message.InitializeNewInstance("android.nfc.NdefMessage", Array(RecordsJO))
    'Write!
    TagTech.RunAsync("WriteNdef", "writeNdefMessage", Array(message), 0)
 
End Sub

The error I get is:
B4X:
activitywritetag_writendef (java line: 3275)
java.lang.IllegalArgumentException: Array has incompatible type: class [Landroid.nfc.NdefRecord;
    at java.lang.reflect.Array.incompatibleType(Array.java:827)
    at java.lang.reflect.Array.set(Array.java:477)
    at anywheresoftware.b4j.object.JavaObject.InitializeArray(JavaObject.java:108)
    at sos.exteryo.com.activitywritetag._writendef(activitywritetag.java:3275)
    at sos.exteryo.com.activitywritetag._ndef_connected(activitywritetag.java:1588)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
    at anywheresoftware.b4a.BA$2.run(BA.java:370)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6723)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

But if I change the line:
B4X:
WriteNdef(recs)
to:

B4X:
WriteNdef(Array(recs(0),recs(1),recs(2),recs(3),recs(4)))
it works, of course limited to a known number of records, from 0 to 4 in the example

The error line is related to:
B4X:
RecordsJO.InitializeArray("android.nfc.NdefRecord", Records)
in the WriteNdef sub

Any suggestion?
Thanks in advance
Davide
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…