Android Question Object should first be initialized (B4XView) when adding B4XSwitch to dynamically created B4XView Panel

amarnath

Member
Licensed User
Longtime User
I am getting a persistent error
: java.lang.RuntimeException: Object should first be initialized (B4XView) at the AddView line.
I am dynamically creating a Card (B4XView) and trying to add a B4XSwitch (and native Labels) to it.
Here is the code snippet:

B4X:
Dim Card As B4XView = xui.CreatePanel("")
Card.SetColorAndBorder(0xFFFFFFFF, 1dip, 0xFFD8D8D8, 12dip)
Card.As(Panel).Elevation = 4dip
pnlValveContent.AddView(Card, CardLeft, CardTop, CardWidth, CardHeight)

Dim Sw As B4XSwitch
Sw.Initialize(Me, "Valve" & i)
Sw.Value = False
Card.AddView(Sw.mBase, CardWidth - 58dip, 59dip, 48dip, 40dip) ' <--- CRASHES HERE


I have also tried Card.As(Panel).AddView(Sw.mBase.As(View), ...) and native CheckBox, but the "Object should first be initialized" error persists.
Is there a specific rule for adding views to a dynamically created xui.CreatePanel in B4A? Am I missing a required class initialization?
 
Top