Android Question Type and Map in different modules

SteveTerrell

Active Member
Licensed User
Longtime User
I have a NameAndMac Type defined in my main activity
Subs, defined in a code module, are used to save and recall an object of this type from a map stored in a file. My Main code also includes a check (for the purpose of this question) that the map can put and get the Type object.

Main:
B4X:
Sub Process_Globals
    Type NameAndMac (name As String, mac As String)
End Sub
Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim map1 As Map
    Dim saveNameAndMac As NameAndMac
    Dim readNameAndMac As NameAndMac
   
    map1.Initialize
    saveNameAndMac.Initialize
    readNameAndMac.Initialize
   
    saveNameAndMac.name = "bt"
    saveNameAndMac.mac = "57"
   
    map1.Put("key",saveNameAndMac)
    readNameAndMac = map1.Get("key")
   
    BluetoothSettingsFile.SaveNewConnection(readNameAndMac)
    saveNameAndMac = BluetoothSettingsFile.SavedMacName
End Sub


The code module has:
B4X:
Sub Process_Globals
    Dim settingsFileName As String = "Settings.txt"
End Sub
Sub SavedMacName As NameAndMac
    Dim settingsMap As Map
    Dim btInfo As NameAndMac
   
    btInfo.Initialize
    If File.Exists(File.DirInternal, settingsFileName) Then
        settingsMap.Initialize
        settingsMap = File.ReadMap(File.DirInternal, "Settings.txt")
       
        If settingsMap.ContainsKey("btInfo") Then
            btInfo = settingsMap.Get("btInfo")
        End If
    End If
    Return btInfo
End Sub
Sub SaveNewConnection(newConnection As NameAndMac)
    Dim settingsMap As Map
    settingsMap.Initialize
    settingsMap.Put("btInfo",newConnection)
    File.WriteMap(File.DirInternal, "Settings.txt",settingsMap)
End Sub


The map operations in Main work ok, The code module call fails and appears not to properly understand the object type.

Installing file.
PackageAdded: package:b4a.example
** Activity (main) Create, isFirst = true **
bluetoothsettingsfile_savedmacname (B4A line: 16)
btInfo = settingsMap.Get("btInfo")
java.lang.ClassCastException: java.lang.String cannot be cast to b4a.example.main$_nameandmac
at b4a.example.bluetoothsettingsfile._savedmacname(bluetoothsettingsfile.java:56)
at b4a.example.main._activity_create(main.java:348)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at b4a.example.main.afterFirstLayout(main.java:98)
at b4a.example.main.access$100(main.java:16)
at b4a.example.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)


Am I doing something wrong?
 
Top