Android Question Initialize an array of labels say

Martincg

Member
Licensed User
The following compiles ok but if I don't comment out the for/next loop then the app crashes as soon as the line marked is executed.

B4X:
Dim lbl1 as label
Dim lbl2 as label

Dim lblArray() As Label 
lblArray = Array As Label(lbl1, lbl2)


Dim ii As int

For ii = 0 to 1
 lblArray(ii).Width = Panel6.Width/5   '<----crash here (tried initializing panel 6 first)
Next
[code/]

Can someone tell me how I should do it please.

Also, how should I mark a section in my post as code? (obviously I'm doing it wrong because these last lines are not supposed to be part of the code.
 

stevel05

Expert
Licensed User
It's difficult without knowing what the error message you're getting is, it's easier to help if you post the error message, or better still, zip(from the file menu) and post an example project.

As I can't check the code I need to ask questions before I can help, So, have you loaded a layout with Panel6 on it or defined it in code?

To enclose code use [ CODE][ /CODE] without the spaces.
 

Martincg

Member
Licensed User
I designed the panel with 7 labels on it. I want to be able to move the labels around so I made an array of them. All the labels have been declared.
So the actual code has more labels than the example I showed. I'll make a small example program and zip that as soon as I can.
The error which shows in the logs is

Error occurred on line: 62 (program)
java.lang.NullPointerException
at anywhere.....


I tried to upload a bitmap screen shot but I get error extension not allowed!
 

Martincg

Member
Licensed User
I made an example which is attached. the code is
B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Private Label1 As Label
   Private Label2 As Label
   Dim labArray() As Label
   labArray = Array As Label(Label1, Label2)
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
   Dim ii As Int
   For ii = 0 To 1
    labArray(ii).Initialize("") ' tried without this line first and had an error that object should be intialized first
    labArray(ii).Width = 120
   Next


End Sub
 

Attachments

stevel05

Expert
Licensed User
The main problem is that you cannot access any of the views layout fields until it has been added to a parent.

The array has two entries, Arrays and lists in the B4x are 0 based, so to access them labArray(0) is the first and labArray(1) is the second. you are accessing labArray(2) with your code which would give an error. A better solution is to use:

B4X:
For ii = 0 to lblArray.Length - 1
You should not allocate more than Strings and Numbers within the Globals Sub.

You shouldn't initialize a Label more than once, in this situation.

This Code runs, but you need to address the issue of adding to a parent beofre your code will work.:

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

    Private Label1 As Label
    Private Label2 As Label
    Dim labArray() As Label

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Label1.Initialize("labs")
    Label2.Initialize("labs")
    'You can also redim a Global Variable
    Dim labArray() As Label = Array As Label(Label1, Label2)
    Dim ii As Int
    For ii = 0 To 1
     'labArray(ii).Initialize("")
     'labArray(ii).Width = 120
     Log(labArray(ii))
    Next


End Sub
 

Martincg

Member
Licensed User
Thanks for your replies Stevel05.
I can get what you showed to run. The designed labels have a parent but as soon as I try to add a line to set the width then the program crashes.

So the next question is "How to you create and then use an array of labels even if you don't set the elements to designed labels, because there is no Parent property shown for the label array?
 

stevel05

Expert
Licensed User
To answer your question first you have to add them to an activity or panel.

As for your program crashing it's back to my first question then, have you loaded the layout with the labels on them?
 

stevel05

Expert
Licensed User
OK, I've just looked again at the app you posted, there is a layout but you haven't loaded it.

Try This:

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

    Private Label1 As Label
    Private Label2 As Label
    Dim labArray() As Label
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("labarrayproblem1")
   
    Dim labArray() As Label = Array As Label(Label1, Label2)
    Dim ii As Int
    For ii = 0 To 1
   
    labArray(ii).Width = 120Dip
    Log(labArray(ii).Width)
    Next
   

End Sub
And you don't need to initialize views that are in a loaded layout.
 
Top