Android Question View left and top

MitchBu

Well-Known Member
Licensed User
Longtime User
I am working on moving views, but I do not succeed getting left and top from views added in the Designer.

Here is what I get with Label1 view.

I made sure to generate the member, and to initialize it in Activity_Create.

I added click to Label1, here is what I placed in there. So indeed the program knows about Label1, and implements the click event :
B4X:
Sub Label1_Click
    Log("Label1.Text: " & Label1.Text)
    Log(Label1.Left)
End Sub

Label1.text does not show, and Label1.Left triggers the following exception :

main_label1_click (B4A line: 121)
Log(Label1.Left)
java.lang.NullPointerException: Attempt to read from field 'int anywheresoftware.b4a.BALayout$LayoutParams.left' on a null object reference
../..

What am I missing ?
 

klaus

Expert
Licensed User
Longtime User
I made sure to generate the member, and to initialize it in Activity_Create.
What do you mean with ...and to initialize it in Activity_Create?
If you added Label1.Initialize("Label1") in Activity_Create it is wrong!
Views added in the Designer must not be initialized in the code!
 
Last edited:
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
It has been declared?

B4X:
Private Label1 as Label

Is initialized?

B4X:
Label1.Initialize("Label1")

It was anchored?

B4X:
Activity.AddView(Label1,0dip,0dip,100dip,40dip)

try also

B4X:
Sub Label1_Click
  Dim La1 as Label

  La1=Sender
  Log("La1.Text: " & La1.Text)
  Log(La1.Left)End Sub
End sub
 
Upvote 0

MitchBu

Well-Known Member
Licensed User
Longtime User
Hi Klaus,

I did declare and initialize the view. I did not need to anchor it since it was added in the Designer.

You last snippet did the trick. For some reason, I cannot access Label1 properties directly, but I can when using la1 reference.

Now my issue is, I need to access different views left and top as well as content in order to print.

The way I do it in another language is to place references to all views in a list (array), so I can sequentially read positions and content in order to send that to the printer.

Unfortunately, I tried to go through an intermediary variable but I still get the null exception. It looks as if I cannot access the real instance of the view.

This triggers the null object exception :
B4X:
Dim laview As View
    laview = Label1
    Log(laview.Left)

Eventually, I will also need to manipulate left and top in order to move controls with touch events to enable user to customize the layout.

So I need to find a way to access reliably each view's properties values, as well as set them.

Thank you
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I did declare and initialize the view.
Views defined in a layout file MUST NOT be initialized in the code!
You MUST remove Label1.Initialize("Label1") !
You get the number of views in an Activity or a Panel with Activity.NumberOfViews or Panel.NumberOfViews and access them with GetView.
Like Panel,GezView(0) etc.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…