Android Question Accessing Main Globals panel data in another code module

Vandermyer

New Member
Licensed User
Longtime User
Within Globals (in Main) a Public Panel is defined. The Activity_Create Sub performs some initialisation that includes calling a Sub in a code module that uses this Panel (accessed via Main.pnlMain). I find that sometimes when trying to access Main.pnlMain I get a exception, and sometimes I don't. The code looks something like this (slightly edited to removed code that does not use pnlMain):

B4X:
Sub Globals

'These global variables will be redeclared each time the activity is created.


'These variables can only be accessed from this module.


Public pnlMain As Panel


End Sub





Sub Activity_Create(FirstTime As Boolean)



Activity.LoadLayout("MainLayout")


pnlMain.Width = Activity.Width


pnlMain.Height = Activity.Height


Base.Initialise




End Sub





Sub Initialise()


Dim myds As Rect


myds.Initialize(xpos, Main.pnlMain.Height - 10%y, xpos + 6%x, Main.pnlMain.Height)


End Sub

1) If you look at the screen image 'Main null' it can be seen that hovering on Main in debug mode reveals that Main is (apparently) null, but data is displayed for Main in the watch window and it can also be seen that the line with the breakpoint has been successfully stepped over.
2) In the image 'Exception' a null object reference has occurred.

Why does this happen? What am I missing? It seemed to me (although this is a bit subjective) that the null exception tended to occur more when the Initialise sub was called earlier the Activity_Create and when the phone's orientation changed.

Any thoughts or ideas appreciated!
 

Attachments

  • Main null.jpg
    Main null.jpg
    282 KB · Views: 169
  • Exception.jpg
    Exception.jpg
    317 KB · Views: 189

Mark Read

Well-Known Member
Licensed User
Longtime User
If I understand you correctly, you have made a small mistake:

Main.pnlMain can only be called outside of the activity main when the pnlMain is declared in Process Globals not Globals. Globals means only for this activity.
 
Upvote 0

Vandermyer

New Member
Licensed User
Longtime User
Thanks for the fast reply.

Main,pnlMain is a Panel, so cannot be defined in Process_Globals - doing so will give the error:

Error description: Cannot access activity object from sub Process_Globals.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You cannot access an Activity object directly from outsides the modules where it is declared.
In a code module you can pass objects in the calling routine.
In your case:
B4X:
Sub Initialize(pnl As Panel)
'
'
myds.Initialize(xpos, pnl.Height - 10%y, xpos + 6%x, pnl.Height)
 
Upvote 0

Vandermyer

New Member
Licensed User
Longtime User
You cannot access an Activity object directly from outsides the modules where it is declared.
In a code module you can pass objects in the calling routine.
In your case:
B4X:
Sub Initialize(pnl As Panel)
'
'
myds.Initialize(xpos, pnl.Height - 10%y, xpos + 6%x, pnl.Height)
Thanks. That has resolved the issue.

It would be nice if the compiler could enforce the restriction that an Activity variable cannot be accessed outside the module where it is declared.
 
Upvote 0
Top