Android Question Undeclared variable 'activity' is used before it was assigned any value.

aedwall

Active Member
Licensed User
Longtime User
When running my project WITHOUT B4XPages, this works just fine:

B4X:
Sub Activity_Create(FirstTime As Boolean)
  Dim UR As Int
 
  Activity.LoadLayout("Layout")
  Activity.LoadLayout("options_panel1")
  Activity.LoadLayout("options_panel2")
 
  cd1.Initialize(Colors.RGB(253, 251, 186), 5dip)      'sets up rounded corners
  cd2.Initialize(Colors.RGB(253, 251, 186), 5dip)
 
  Bmp101.Initialize(File.DirAssets, "calendar.png")
  Bmp102.Initialize(File.DirAssets, "clock.png")

  IME1.Initialize("IME")

  p3.Initialize("p3")

  Activity.AddView(p3, 0, 0, 100%x, 100%y - 60dip)

But when I convert to using B4XPages, and use the below, I get an error message:

Parsing code. Error
Error parsing program.
Error description: Undeclared variable 'activity' is used before it was assigned any value.
Error occurred on line: 65 (B4XMainPage)
Activity.LoadLayout("options_panel1")

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
  Dim txt As String
  Dim UR As Int
  Dim FirstTime As Boolean

  Root = Root1
  Root.LoadLayout("MainPage")

  Activity.LoadLayout("options_panel1")
  Activity.LoadLayout("options_panel2")
  
  cd1.Initialize(Colors.RGB(253, 251, 186), 5dip)      'sets up rounded corners
  cd2.Initialize(Colors.RGB(253, 251, 186), 5dip)
  
  Bmp101.Initialize(File.DirAssets, "calendar.png")
  Bmp102.Initialize(File.DirAssets, "clock.png")

  IME1.Initialize("IME")

  p3.Initialize("p3")

  Activity.AddView(p3, 0, 0, 100%x, 100%y - 60dip)

What do I need to do to fix this error?

The documentation says this:

You can also call a second layout from within the first Activity, using Activity.LoadLayout("Layout2").

But that doesn't seem to work.

Thank you.
 

Alessandro71

Well-Known Member
Licensed User
Longtime User
Create a new empty project with B4XPages template and you’ll see how to use LoadLayout in that context.
you don’t need to address Activity anymore
 
Upvote 0

aedwall

Active Member
Licensed User
Longtime User
Create a new empty project with B4XPages template and you’ll see how to use LoadLayout in that context.
you don’t need to address Activity anymore
I have done this and it tells me nothing that I do not already show, which gives errors when compiling. So something is needed.

After I started the new project, I added some code to do what I need. But it won't compile. So what is wrong?

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI

  Dim Activity As Activity
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")

  Activity.LoadLayout("options_panel1")
  Activity.LoadLayout("options_panel2")
 
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")

The errors are (it does not like "Dim Activity As Activity", but if I comment that out, then it says that Activity is an undeclared variable. So I can't seem to win.:

Compiling debugger engine code. Error
B4A line: 12
Dim Activity As Activity
shell\src\b4a\example\b4xmainpage_subs_0.java:64: error: cannot find symbol
b4xmainpage._activity = RemoteObject.createNew ("anywheresoftware.b4a.objects.ActivityWrapper");__ref.setField("_activity",b4xmainpage._activity);
^
symbol: variable _activity
location: class b4xmainpage
1 error
only showing the first 1 errors, of 2 total; use -Xmaxerrs if you would like to see more

When 'Dim Activity As Activity' is commented out, then I see this error:

Parsing code. Error
Error parsing program.
Error description: Undeclared variable 'activity' is used before it was assigned any value.
Error occurred on line: 24 (B4XMainPage)
Activity.LoadLayout("options_panel1")
 
Upvote 0

aedwall

Active Member
Licensed User
Longtime User
Replace Activity by Root.
That worked. Thank you. I thought I actually tried that in my production project and it didn't work, hence I kept looking for another solution. But today it seems to work, except for some minor coloring issues.
 
Upvote 0
Top