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

Waiting for debugger to connect...
Program started.
Error occurred on line: 150 (B4XMainPage)
java.lang.RuntimeException: Object should first be initialized (B4XView).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:49)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:64)
at b4j.example.b4xmainpage._b4xpage_created(b4xmainpage.java:137)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:115)
at anywheresoftware.b4a.debug.Debug.CallSubNew2(Debug.java:81)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.keywords.Common.CallSubDebug2(Common.java:486)
at b4j.example.b4xpagesmanager._createpageifneeded(b4xpagesmanager.java:872)
at b4j.example.b4xpagesmanager._showpage(b4xpagesmanager.java:351)
at b4j.example.b4xpagesmanager._addpage(b4xpagesmanager.java:172)
at b4j.example.b4xpagesmanager._addpageandcreate(b4xpagesmanager.java:186)
at b4j.example.b4xpagesmanager._initialize(b4xpagesmanager.java:122)
at b4j.example.main._appstart(main.java:80)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at b4j.example.main.start(main.java:38)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
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:174)
at java.base/java.lang.Thread.run(Thread.java:834)
 

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
Top