Android Question [B4XPages] Warning Views not found Load Layout using String from a Code Module

rwblinn

Well-Known Member
Licensed User
Longtime User
In a B4XPages project added a code module holding constants.
In the code module, declared the B4XPages ID like:
B4X:
Public Const MAINPAGE_ID As String = "MainPage"
...

When setting the constant as parameter in load layout (in B4XMainPage), the views of the layout are not found (log message).

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Button1 As B4XView
    Private Label1 As B4XView
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    ' Using the constant string, the log shows:
    ' B4XMainPage - 11: Variable 'Button1' is never assigned any value. (warning #10)
    ' B4XMainPage - 12: Variable 'Label1' is never assigned any value. (warning #10)
    Root.LoadLayout(Constants.MAINPAGE_ID)
    
    ' Tried this (declared a sub returning the constant) but same error
    ' Root.LoadLayout(Constants.Get_MainPage_ID)
    
    ' Using the string direct, no issue
    'Root.LoadLayout("MainPage")
End Sub

Not sure if this is a bug or if constants needs to be declred different.
 

Attachments

  • Project.zip
    14.3 KB · Views: 79

DonManfred

Expert
Licensed User
Longtime User
with

B4X:
Root.LoadLayout(Constants.MainPageID)

And in constants
B4X:
Public Sub getMainPageID As String
    Return MAINPAGE_ID ' MAINPAGE_ID does not need to be PUBLIC
End Sub

Works as expected...

Even you code does work. The sub is not needed when the variable is public.

What exactly is the problem?
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Thanks for steer.
The problem is when using the constant from the code module Constants:
B4X:
Sub Process_Globals
    Public Const MAINPAGE_ID As String = "MainPage"
End Sub
in B4XMainPage Root.LoadLayout:
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
  Root.LoadLayout(Constants.MAINPAGE_ID)
...
the B4A log shows a warning that the views of the layout are never assigned any value:
B4X:
B4XMainPage - 11: Variable 'Button1' is never assigned any value. (warning #10)
B4XMainPage - 12: Variable 'Label1' is never assigned any value. (warning #10)
When using
B4X:
Root.LoadLayout("MainPage")
no warnings.

NOTE: The constant can be used to assign to a text of a label or as parameter in own subs. Seems an issue with LoadLayout.
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
I think that is is because the IDE uses the Layout names in loadlayout to check which objects are in the layout and flag them as being initalised. By using a constant (or a variable) for the layout name, you prevent this from happening.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
...because the IDE uses the Layout names in loadlayout to check which objects are in the layout and flag them as being initalised...
As the application working upon target is B4A & B4J with same layout, checked also with B4J = same behaviour.
Not only using Root.LoadLayout in the B4XMainpage but also in other B4XPages, like a ConfigPage created.

Why using Constants with the Page ID's?
These are used to load the layout and to call B4XPages from f.e. button event, like:
B4X:
B4XPages.ShowPage(Constants.CONFIGPAGE_ID)
instead hardcoding the several Page ID's:
B4X:
B4XPages.ShowPage("ConfigPage")
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
The same behavior happens with B4i as well.

I agree with your reasoning for using constants. However, by doing so it seems that the IDE is unable to identify layout elements as being initialized.

So I suppose there is a choice to be made.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
The IDE can only do static code analysis to identify views initialised in layouts.. It can't identify them if you use a variable whose value is only known for sure at runtime. A constant is essentially a read-only variable so for convenience and rapidity in parsing the IDE treats constants as normal variables for this use..
 
Upvote 0
Top