For a while I've been noticing an error when I reload a CustomListView quite quickly:
Each CLV item contains a B4XCombobox and the error seems to be because I'm trying to use clv.GetItemFromView using the underlying combobox.
Attached is a small project showing the issue. If you run it and click the button quickly then you'll see in the logs:
Without the Try.... Catch the full error generated is:
The relevant code in the example app is
The try catch does its job and in the real app there don't seem to be any bad side effects, but can anyone tell me why this is occuring and perhaps if there's a better way to avoid the error than the Try...Catch?
Many thanks as always.
Each CLV item contains a B4XCombobox and the error seems to be because I'm trying to use clv.GetItemFromView using the underlying combobox.
Attached is a small project showing the issue. If you run it and click the button quickly then you'll see in the logs:
B4X:
Error caught in cmb1_SelectedIndexChanged.
(Possibly occurs when quickly reloading the clv):
java.lang.RuntimeException: Object should first be initialized (B4XView).
Without the Try.... Catch the full error generated is:
B4X:
customlistview._getitemfromview (java line: 415)
java.lang.RuntimeException: Object should first be initialized (B4XView).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:49)
at b4j.example.customlistview._getitemfromview(customlistview.java:415)
at b4j.example.main._cmb1_selectedindexchanged(main.java:97)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:514)
at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:469)
at b4j.example.b4xcombobox$ResumableSub_RaiseEvent.resume(b4xcombobox.java:272)
at anywheresoftware.b4a.keywords.Common$2$1.run(Common.java:1051)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:833)
The relevant code in the example app is
B4X:
Sub Button1_Click
clv.Clear
For i=0 To Rnd(3, 10)
clv.Add(CreateclvItem(clv.AsView.Width, 40), i)
Next
End Sub
Private Sub CreateclvItem(Width As Double, Height As Double) As B4XView
Dim p As B4XView = xui.CreatePanel("")
p.SetLayoutAnimated(0, 10, 0, Width, Height)
p.LoadLayout("clvItem")
txt.Text = "text here"
cmb1.setItems(Array As String("item1", "item2", "item3", "item4", "item5"))
cmb1.cmbBox.Value="item3"
Return p
End Sub
Private Sub cmb1_SelectedIndexChanged (Index As Int)
Dim cmb As B4XComboBox = Sender
Dim cmbbox As B4XView = cmb.cmbBox
'try...catch... added to preventjava.lang.RuntimeException: Object should first be initialized (B4XView) occuring at
'Seems to occur when reloading the clv too quickly?
Try
Log(cmb.SelectedItem)
Dim s As String = clv.GetValue(clv.GetItemFromView(cmbbox)) <----error here
Log(s)
Catch
Log($"Error caught in cmb1_SelectedIndexChanged.
(Possibly occurs when quickly reloading the clv):
${LastException.Message}"$)
End Try
End Sub
The try catch does its job and in the real app there don't seem to be any bad side effects, but can anyone tell me why this is occuring and perhaps if there's a better way to avoid the error than the Try...Catch?
Many thanks as always.