Android Question Error using custom type

Pedro Caldeira

Active Member
Licensed User
Longtime User
Hello guys, need a quick help here. Surely I am missing something.
I have a custom type and its CreateSub.
Then I have a map, with a key and this custom type filled with the propper variables as value
When I want to retrieve the values, I get the type (In debug i can see its contents and is correct), but when I want to assign the elements to variables I get an error
What am I missing here ?

THE ERROR

B4X:
Error occurred on line: 4993 (AppDialogs)
java.lang.RuntimeException: Field: dataDesde not found in: java.lang.String
    at anywheresoftware.b4a.shell.Shell$FieldCache.getField(Shell.java:923)
    at anywheresoftware.b4a.shell.Shell.getField(Shell.java:697)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:360)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:22)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.agraham.dialogs.InputDialog$ExtendedBALayout$1.run(InputDialog.java:2467)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:247)
    at android.app.ActivityThread.main(ActivityThread.java:8656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

THE CODE THAT TRIGGERS THE ERROR ( lblAlertaDesde.Text = MapaFiltro.dataDesde)

B4X:
If File.Exists(Starter.InternalFolder, "alertprefs.def") Then
        ShareCode.FilterPreferences.Clear
        Try
            ShareCode.FilterPreferences = File.readMap(Starter.InternalFolder, "alertprefs.def")
            If ShareCode.FilterPreferences.ContainsKey(alertType) Then
                Dim MapaFiltro As FilterPreferences = ShareCode.FilterPreferences.Get(alertType)
               
                lblAlertaDesde.Text = MapaFiltro.dataDesde
                lblAlertaAte.Text = MapaFiltro.dataAte
                butAlertDiarios.Checked = MapaFiltro.BotaoDia
                butAlertSemana.Checked = MapaFiltro.BotaoSemana
                butAlertMes.Checked = MapaFiltro.BotaoMes
                butAlertDefinir.Checked = MapaFiltro.BotaoDefinir
                DialogTypeAlert.SelectedIndex = MapaFiltro.TipoAlerta
                DialogStatusAlert.SelectedIndex = MapaFiltro.EstadoAlerta
            End If
        Catch
            Log(LastException)
        End Try
    End If

THE TYPE

B4X:
Type FilterPreferences (TipoAlerta As Int, EstadoAlerta As Int, dataDesde As String, dataAte As String, BotaoDia As Boolean, BotaoSemana As Boolean, BotaoMes As Boolean, BotaoDefinir As Boolean)

Public Sub CreateFilterPreferences (TipoAlerta As Int, EstadoAlerta As Int, dataDesde As String, dataAte As String, BotaoDia As Boolean, BotaoSemana As Boolean, BotaoMes As Boolean, BotaoDefinir As Boolean) As FilterPreferences
    Dim t1 As FilterPreferences
    t1.Initialize
    t1.TipoAlerta = TipoAlerta
    t1.EstadoAlerta = EstadoAlerta
    t1.dataDesde = dataDesde
    t1.dataAte = dataAte
    t1.BotaoDia = BotaoDia
    t1.BotaoSemana = BotaoSemana
    t1.BotaoMes = BotaoMes
    t1.BotaoDefinir = BotaoDefinir
    Return t1
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
ShareCode.FilterPreferences = File.readMap(Starter.InternalFolder, "alertprefs.def")
This does not work! File.ReadMap does return a map with STRINGs. There is no CustomType in the resulting map.

Reads the file and parses each line as a key-value pair (of strings).
 
Upvote 1

Pedro Caldeira

Active Member
Licensed User
Longtime User
But why not, I am just reading a map !?
FilterPreferences is a map, in another common module (ShareCode)
B4X:
ShareCode.FilterPreferences = File.readMap(Starter.InternalFolder, "alertprefs.def")

later, when I get the error I am assigning the value of the map to a custom type, not the map
B4X:
Dim MapaFiltro As FilterPreferences = ShareCode.FilterPreferences.Get(alertType)

Could be the equal name FilterPreferences beetween the map and the custom type ? Going to change it
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
DonManfred is correct. ReadMap constructs a Map with strings as both keys and values according to the Java Properties conventions. The Intellisense for ReadMap contains this link.
Your value is a String containing a String representation of your custom type. You will need to further parse it for the individual entries.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Solution
B4X:
    Dim ser As B4XSerializator

write the customtype with
B4X:
Dim drbytes() As Byte = ser.ConvertObjectToBytes(yourcustomtypeobject)
File.WriteBytes(File.DirApp,"mytype.dat",drbytes)
and read it back with
B4X:
Dim drbytes() As Byte = File.ReadBytes(File.DirApp,"mytype.dat")
Dim yournewobject As FilterPreferences = ser.ConvertBytesToObject(drbytes)
 
Upvote 2
Top