B4J Question Initialize and use an array of B4XViews by code

max123

Well-Known Member
Licensed User
Longtime User
Hi programmers...

I have a B4XPages cross platform project where I initialize by code an array of B4XViews that should be EditText on B4A and TextField on B4J:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    
    Public Text(15) As B4XView  ' Should be EditText on B4A and TextField on B4J

Next I initialize these this way in B4XPages_Created:
B4X:
    For i = 0 To Text.Length-1

#If B4A
        Text(i).As(EditText).Initialize("Text")
#Else If B4J
        Text(i).As(TextField).Initialize("Text")  ' <<<<< CRASH HERE ON INITIALIZER
#End If       
        Text(i).TextSize = 12
        Text(i).TextColor = xui.Color_Yellow
#If B4A       
        Text(i).As(EditText).Gravity = Gravity.CENTER
#Else If B4J
        Text(i).As(TextField).Style = Text(i).As(TextField).Style & "-fx-text-alignment: center;"
#End If
#If B4A       
        Text(i).As(EditText).SingleLine = True
#End If   
        Text(i).Text = PageButtonSettings.GetText(i)
#If B4A           
        Root.AddView(Text(i), Led(i).Right, Led(i).Top-3%x, 24%x, 12%x)
#Else If B4J
        Root.AddView(Text(i), Led(i).Right, Led(i).Top-(w*3/100), (w*24/100), (w*12/100))
#End If       
        
        Text(i).Tag = i
        Text(i).Enabled = False
    Next
But as my comment, it crash on the initializer. The error says that it should be previously inizialized.... but in the initializer itself ?

1) How I can initialize if initialize itself crash ?
2) Is the cast wrong here ?

Many thanks

 

max123

Well-Known Member
Licensed User
Longtime User
I Erel thanks for fast reply, I need to use TextField and EditText, but the most important is that I want to know how do it and manage with all other no cross platform controls, so if a cast is a good option here or I've to reference it in Globals this way:
B4X:
#If B4A 
    Public Text(15) As EditText
#Else If B4J
    Public Text(15) As TextField
#End If
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Maybe
B4X:
#Else If B4J
        Dim tmp as TextField
        tmp.Initialize("Text")
        Text(i) = tmp
...
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Thanks @Daestrum, this worked:
B4X:
#Else If B4J
        Dim tmp As TextField
        tmp.Initialize("Text")
        Text(i) = tmp
        Text(i).TextSize = 12
        Text(i).TextColor = xui.Color_Black       
        Text(i).SetTextAlignment("CENTER", "CENTER")
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…