Android Question Array of Label

Luki

New Member
Licensed User
Longtime User
Hi all,

Again a beginner's question.
I want to create an array of Label but the number of Label is only known after a certain point in Sub Activity_Create.
If the number of label is known in advance (in this case 6) then this code below works:

B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim lblWoord() As Label
   Dim Lbl1, Lbl2, Lbl3, Lbl4, Lbl5, Lbl6 As Label

End Sub

Sub Activity_Create
...
lblWoord = Array As Label(Lbl1, Lbl2, Lbl3, Lbl4, Lbl5, Lbl6)
...
For i = 0 To 5
     lblWoord(i).Initialize("lblWoord")
     lblWoord(i).TextColor = Colors.black
     lblWoord(i).TextSize = 16
...
...
Next

End Sub

How to do this when the number of labels (Lbl1 .. Lbl6) is not known before hand but only after a certain point in Sub Activity_Create? In other words, is it possible to do

B4X:
Dim Lbl1, Lbl2, Lbl3, Lbl4, Lbl5, Lbl6 As Label
and
B4X:
lblWoord = Array As Label(Lbl1, Lbl2, Lbl3, Lbl4, Lbl5, Lbl6)

in an iterative manner after the number of Label is known?


Thanks!
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Something like this.

B4X:
Sub Globals
     Dim lblWoord() As Label
End Sub

Sub Activity_Create
    'When you Know how many there are
    Dim Count As Int = 10
    Dim lblWoord(Count) As Label                'still a global variable
    For i = 0 To Count-1
        Dim L As Label
        L.Initialize("lblWoord")
        L.TextColor = Colors.black
        L.TextSize = 16
        L.Tag = i
        lblWoord(i) = L
    Next
   
    For Each L As Label In lblWoord
        Log(L.Tag)
    Next
End Sub
 
Upvote 0

mike2506

Member
Licensed User
Longtime User
B4X:
Sub Globals
     Dim lblWoord() As Label
End Sub

Sub Activity_Create
    'When you Know how many there are
    Dim Count As Int = 10
    Dim lblWoord(Count) As Label                'still a global variable
    For i = 0 To Count-1
        Dim L As Label
        L.Initialize("lblWoord")
        L.TextColor = Colors.black
        L.TextSize = 16

        L.Left=0

        L.Tag = i
        lblWoord(i) = L
    Next
  
    For Each L As Label In lblWoord
        Log(L.Tag)
    Next
End Sub

When the statement L.Left=0 is executed this error is reported in the log:
main_gps_gpsstatus (B4A line: 356)
L.Left=0
java.lang.NullPointerException
at anywheresoftware.b4a.objects.ViewWrapper.setLeft(ViewWrapper.java:156)
at raccolta.rifiuti.com.main._gps_gpsstatus(main.java:1739)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.gps.GPS$2.onGpsStatusChanged(GPS.java:99)
at android.location.LocationManager$GpsStatusListenerTransport$1.handleMessage(LocationManager.java:1468)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException

Please help me for resolve this problem.
Thanks.
 
Upvote 0
Top