Android Question [B4XPreferencesDialog] App crash when editing date in a DateItem, unparsable date format

Vulcano Team Software

New Member
Licensed User
Hi,
I was looking for a nice dialog to use for a form asking the user to input both the date and the time of its order, and I step upon the library in the title. Sadly I never managed to get the date field to work. The App crash, and looking at the logs this is what I get:
*** Service (webservicechecker) Create ***
** Service (webservicechecker) Start **
Error occurred on line: 842 (PreferencesDialog)
java.text.ParseException: Unparseable date: "2021-11-03"
at java.text.DateFormat.parse(DateFormat.java:362)
at anywheresoftware.b4a.keywords.DateTime.DateParse(DateTime.java:148)
at vulcano.risto.preferencesdialog._lbldate_click(preferencesdialog.java:2998)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
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.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6611)
at android.view.View.performClickInternal(View.java:6584)
at android.view.View.access$3100(View.java:783)
at android.view.View$PerformClick.run(View.java:26246)
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:6702)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)
I supposed the problem was the Datetime format I was using, but even setting the platform default I keep getting the same error.
This is the code that should initialize and show the dialog, I left comments in code to show the many tries I done to get it work
B4X:
public Sub dialogo_data()
    Dim dialogo As PreferencesDialog
    Dim output As Map
    Dim time_op As String = DateTime.DateFormat
'   
    DateTime.DateFormat = "yyyy-MM-dd"
    'DateTime.DateFormat = DateTime.DeviceDefaultDateFormat
    'DateTime.TimeFormat = DateTime.DeviceDefaultTimeFormat
'    time_op.Add("24")
    output.Initialize
    dialogo.Initialize(Activity, "test", Activity.Width *0.7, Activity.Height *0.6)
    dialogo.LoadFromJson(File.ReadString(File.DirAssets, "test.json"))
'    dialogo.DateTemplate.Initialize
'    dialogo.DateTemplate.FirstDay = 1
'    dialogo.DateTemplate.Date = DateTime.Now
'    dialogo.AddDateItem("data", "data ora")
'    dialogo.AddExplanationItem("","trololoo", "ahsdjsjaj")
'    dialogo.AddTimeItem("tempo", "tempo_ora")
'    dialogo.SetOptions("tempo", time_op)
    
    Wait For (dialogo.ShowDialog(CreateMap(), "OK", "CANCEL")) Complete(Result As Int)
    If Result = xui.DialogResponse_Positive Then
        Log("data: " & output.Get("data_item"))
        Log("tempo: " & output.Get("time_item"))
    End If
    'DateTime.DateFormat = time_op
End Sub

We are using B4A version 11.0 and B4XPreferencesDialog version 1.74
 

josejad

Expert
Licensed User
Longtime User
Software Development Manager don't let me distribute the whole source for any reason
Can you reproduce it in a small project?
Create a new project, add the B4XPreferencesDialog and test it. If you get the error, upload it

 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I guess I will need to look to an alternative solution.
You don't need to look to an alternative. PreferenceDIalog works
Here is a complete working example based on your code. You can make a stand alone project and test:
B4X:
public Sub dialogo_data() 
    Dim dialogo As PreferencesDialog
    Dim output As Map
    Dim time_op As String = DateTime.DateFormat
    DateTime.DateFormat = "yyyy-MM-dd"
    output.Initialize
    dialogo.Initialize(Activity, "test", Activity.Width *0.7, Activity.Height *0.6)
    dialogo.AddDateItem("data_item", "data ora")
    dialogo.AddTimeItem("time_item", "tempo_ora")
    output =CreateMap("data_item":Null, "time_item": Null)
    Wait For (dialogo.ShowDialog(output, "OK", "CANCEL")) Complete(Result As Int)
    If Result = xui.DialogResponse_Positive Then
        Dim d As Long =output.Get("data_item")
        Log($"My string Date: ${DateTime.Date(d)}"$)  'exple:My string Date: 2021-11-20
                
        Dim t As Period =output.Get("time_item")
        Log($"String Time: $2.0{t.Hours}:$2.0{t.Minutes}"$)  'exple: String Time: 19:33
    End If
    DateTime.DateFormat = time_op
End Sub
 
Upvote 1
Top