Android Question How b4xpages share data

PABLO2013

Well-Known Member
Licensed User
Longtime User
I have three b4xpages :

main, b4xMainPage(splash) that transfers to B4xpage2, in B4xpage2 i have a button view to start a Start module that contains this method GpsStatus (Satellites As List), how do I pass sb.string to a label view that is located in B4xpage2, thaks in advanced.


B4X:
Public Sub GpsStatus (Satellites As List)
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append("GPS Status:").Append(CRLF)
    Dim NumberOfSatellites As Int = Satellites.Size
    Dim Beidou, Galileo, Glonass, Gps, Other As List
    Beidou.Initialize
    Galileo.Initialize
    Glonass.Initialize
    Gps.Initialize
    Other.Initialize
    Dim usedinfix As Int = 0
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append("GPS Status").Append(CRLF)
    For i = 0 To NumberOfSatellites - 1
        Dim gpsat As GPSSatellite = Satellites.Get(i)
        If gpsat.Prn > 300 Then
            Galileo.Add(i)
        Else If gpsat.Prn > 200 Then
            Beidou.Add(i)
        Else If gpsat.Prn > 64 Then
            Glonass.Add(i)
        Else
            Gps.Add(i)
        End If
        If gpsat.UsedInFix Then usedinfix = usedinfix + 1
    Next
    sb.Append($"Beidou ${Beidou.Size} : "$)
    For i = 0 To Beidou.Size -1
        Dim gpsat As GPSSatellite = Satellites.Get(Beidou.Get(i))
        sb.Append(gpsat.Prn).Append(" ")
    Next
    sb.Append(CRLF)
  
    sb.Append($"Galileo ${Galileo.Size} : "$)
    For i = 0 To Galileo.Size - 1
        Dim gpsat As GPSSatellite = Satellites.Get(Galileo.Get(i))
        sb.Append(gpsat.Prn).Append(" ")
    Next
    sb.Append(CRLF)
  
    sb.Append($"Glonass ${Glonass.Size} : "$)
    For i = 0 To Glonass.Size -1
        Dim gpsat As GPSSatellite = Satellites.Get(Glonass.Get(i))
        sb.Append(gpsat.Prn).Append(" ")
    Next
    sb.Append(CRLF)
  
    sb.Append($"Gps ${Gps.Size} : "$)
    For i = 0 To Gps.Size -1
        Dim gpsat As GPSSatellite = Satellites.Get(Gps.Get(i))
        sb.Append(gpsat.Prn).Append(" ")
    Next
    sb.Append(CRLF)
    sb.Append($"Satellites : ${NumberOfSatellites}"$).Append(CRLF)
    sb.Append($"Used in fix: ${usedinfix}"$).Append(CRLF)
'    lblSatellites.Text = sb.ToString
    Log(sb.ToString)
  
                 Dim Page As B4XPage2
  
                Page.Initialize
  
                Page.Labelgnss.Text=sb.ToString

               'java.lang.RuntimeException: Object should first be initialized (B4XView).
  
  
  
End Sub
 

William Lancee

Well-Known Member
Licensed User
Longtime User
There are two ways.
1. Make a Public Sub loadLabel (value) in page 2 that is called by GpsStatus using pageInstanceName.loadLabel(value)
2. Make the label in page 2 a Public labelName and use pageInstanceNamelabelName.Text = value
 
Last edited:
Upvote 1

PABLO2013

Well-Known Member
Licensed User
Longtime User
Thank you very much Lancce. I did this, the label is in a layout...so when I try to write to it...it says that it is not initialized, however page 2 and its layout are initialized...well I think so..



B4XPage2:
Public Sub loadLabel (value)
Log(value)  ' ok
Labelgnss.Text=value    ' Labelgnss is a layout  B4XPage2 view
                                      ' java.lang.RuntimeException: Object should first be initialized (Label).
                                      '  Did you forget to call Activity.LoadLayout?
                                      ' at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67).....
End Sub

Starter:
Public Sub GpsStatus (Satellites As List)
    Dim Page As B4XPage2
    Page.Initialize
    Page.loadLabel("hola")
End Sub
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
If B4Xpage2 is not yet created then the loadlayout will not have taking place and the label is not inialized.
You also have to
register B4XPage2 with B4XPages.AddPageAndCreate("Page", Page) before you call GspStatus

So before you call GpsStatus:
B4X:
'Before calling GpsStatus
Dim page2 as B4XPage2
page2.Initialize
B4XPages.AddPageAndCreate("page2", page2)  'The create part is where you load the layout
 
Upvote 0

PABLO2013

Well-Known Member
Licensed User
Longtime User
Greetings, I did what was indicated but the same problem continues
As I told you once in B4XPage2 and called the corresponding layoud (Labelgnss, label view), from here with a button the start module is initialized, which has the GpsStatus module in this sending a variable string with the GPS data (B4XPage2... Labelgnss.text=data...but it gives the indicated error, B4XPage2 does not close, it just calls the module and returns the data, thanks
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
To solve this you need to post a small extract that shows the problem as a .zip file.
We don't really know what you are doing.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
It works well. See below and attached. But without seeing what the OP did, we can't see what went wrong in his code, and he may not be further enlightened.

B4X:
'B4XMainPage

#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
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")
    Dim Page2 As MyPage2
    Page2.Initialize
    B4XPages.AddPageAndCreate("Page2", Page2)
    Page2.Label1.Text = "Hello World"
    Sleep(0)
    B4XPages.ShowPage("Page2")
End Sub

B4X:
'MyPage2

Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Public Label1 As Label
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
    Return Me
End Sub

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

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

Attachments

  • PageRefs.zip
    10 KB · Views: 82
Upvote 0
Top