Android Question B4x - Loop through Labels in a List

Hi guys.
I encounterd a problem and I can't seem to figure it out.
Using B4X 13 on Android 15

I created 3 labels in the designer, added the name of the label-views to a list.
Now I want to change the text of each label within a small loop and get an error message...

Could someone here give me hint what I am doing wrong?
Thanks so much.

Here is my code:

===========================================
Labels in a loop:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
   
    Private Label1 As Label
    Private Label2 As Label
    Private Label3 As Label
   
    Public Ranks(3) As Label
    Ranks = Array As Label (Label1, Label2, Label3)
   
   
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
   
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
   
    For i = 0 To 2
        Ranks(i).text = "abc"
    Next
   
End Sub
=============================================================


Here is the error message:
=================

Logger verbunden mit: Google Pixel 8 Pro
--------- beginning of main
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
Error occurred on line: 38 (B4XMainPage)
java.lang.RuntimeException: Object should first be initialized (Label).
Did you forget to call Activity.LoadLayout?
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
at anywheresoftware.b4a.objects.TextViewWrapper.setText(TextViewWrapper.java:39)
at b4a.example.b4xmainpage._button1_click(b4xmainpage.java:78)
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:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:8036)
at android.view.View.performClickInternal(View.java:8013)
at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
at android.view.View$PerformClick.run(View.java:31237)
at android.os.Handler.handleCallback(Handler.java:959)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loopOnce(Looper.java:232)
at android.os.Looper.loop(Looper.java:317)
at android.app.ActivityThread.main(ActivityThread.java:8705)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)
 

walterf25

Expert
Licensed User
Longtime User
Your code is wrong, you need to get the label object from each iteration like this:

You already added the 3 labels in the designer, you can either add the three labels to a list. What you have done is you have declared an array of labels, the error you are getting is because you did not initialize the array of labels. You can do this instead:

B4X:
dim labelViews As List
labelViews.Initialize2(Array As label(label1, label2, label3))

for each l as label
    l.text = "abc"
Next

Walter
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Your tip helped solving this problem! Worked perfectly well...
Don't mark your answer as solution, but the Klaus one, then (no marques como solución tu respuesta, sino la de Klaus)
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…