Android Question Adding a Drawer to existing B4XPage-Project causes JavaObject not initialized-Error

DonManfred

Expert
Licensed User
Longtime User
Hello,

i´m trying to add a B4XDrawer to an existing (relative small) B4XPages project.

My Manifest (App is not on Playstore)

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="28"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
'End of default text.
AddApplicationText(<meta-data
    android:name="com.google.android.gms.vision.DEPENDENCIES"
    android:value="barcode" />
)
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE) ' Allows an application to write to external storage.
CreateResourceFromFile(Macro, Core.NetworkClearText)
In B4XMainPage i have
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    kvs.Initialize(File.DirInternal,"kvs.dat")
    Root = Root1
    Root.LoadLayout("layoutlogin")
    logo.Bitmap = LoadBitmap(File.DirAssets, "kasselhuskieslogo.png")
    imgsnow.Bitmap = LoadBitmap(File.DirAssets, "event-bg.png")
    B4XPages.SetTitle(Me, "Kassel Huskies - Tickets (Login)")



    ScanPage.Initialize
    B4XPages.AddPage("Scanner", ScanPage)

    EventsPage.Initialize
    B4XPages.AddPage("Events", EventsPage) ' Here i want to use the Drawer

    B4XPages.GetManager.LogEvents = True
End Sub

After login the user is pointed to the EventsPage where the error happens.

In Events-Page i do have

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

    'load the layout to Root
    Drawer.Initialize(Me, "Drawer", Root, 200dip)
    Drawer.CenterPanel.LoadLayout("layoutevents")
    Drawer.LeftPanel.LoadLayout("Drawer")
  

    HamburgerIcon = xui.LoadBitmapResize(File.DirAssets, "hamburger.png", 32dip, 32dip, True)
    logo.Bitmap = LoadBitmap(File.DirAssets, "kasselhuskieslogo.png")
    imgsnow.Bitmap = LoadBitmap(File.DirAssets, "event-bg.png")
    B4XPages.SetTitle(Me, "Kassel Huskies - Events")
    '
    clv.Clear
End Sub
Private Sub B4XPage_CloseRequest As ResumableSub
    #if B4A
    'home button
    If Main.ActionBarHomeClicked Then
        Drawer.LeftOpen = Not(Drawer.LeftOpen)
        Return False
    End If
    'back key
    If Drawer.LeftOpen Then
        Drawer.LeftOpen = False
        Return False
    End If
    #end if
    Return True
End Sub

Private Sub B4XPage_Disappear
    #if B4A
    B4XPages.GetManager.ActionBar.RunMethod("setHomeAsUpIndicator", Array(0))
    #end if
End Sub

Private Sub B4XPage_Resize (Width As Int, Height As Int)
    Drawer.Resize(Width, Height)
End Sub


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

public Sub B4XPage_Appear
    #if B4A
    Sleep(0)
    B4XPages.GetManager.ActionBar.RunMethod("setDisplayHomeAsUpEnabled", Array(True))
    Dim bd As BitmapDrawable
    bd.Initialize(HamburgerIcon)
    B4XPages.GetManager.ActionBar.RunMethod("setHomeAsUpIndicator", Array(bd))
    #End If
    Log($"${B4XPages.GetManager.MainPage.GetBaseURL}/upcoming-events"$)
    'B4XPages.GetManager.MainPage.kvs.Get("access_token")
    Log($"Header: "X-Authorization", "Bearer ${B4XPages.GetManager.MainPage.kvs.Get("access_token")}"$)
    Dim j2 As HttpJob
    j2.Initialize("",Me)
    j2.Username = "xxxx"
    j2.Password = "cccc"
    j2.Download2($"${B4XPages.GetManager.MainPage.GetBaseURL}/upcoming-events"$, Array As String("mimeType", "application/json"))
    j2.GetRequest.SetHeader("X-Authorization", "Bearer "&B4XPages.GetManager.MainPage.kvs.Get("access_token"))
  
    Dim elist As List
    elist.Initialize
  
    Dim Eventsheute As Int = 0
  
    Wait For(j2)JobDone(job2 As HttpJob)
    If job2.Success Then
        Dim result As String = job2.GetString
  
        Dim parser As JSONParser
        parser.Initialize(result)
        Dim jRoot2 As List = parser.NextArray
        For Each colj2Root As Map In jRoot2
            B4XPages.GetManager.MainPage.kvs.Put("events",colj2Root)
            Dim eventId As Int = colj2Root.Get("eventId")
            Dim eventName As String = colj2Root.Get("eventName")
            Dim HomeTeam As String = ""
            Dim RivalTeam As String = ""
  
            Dim eventcomponents() As String
            eventcomponents = Regex.Split(" vs ", eventName)
            If eventcomponents.Length = 2 Then
                HomeTeam = eventcomponents(0)
                RivalTeam = eventcomponents(1)
            End If
  
  
            Dim match As Map = colj2Root.Get("match")
            Dim teamLogo As String = match.Get("teamLogo")
            If File.Exists(File.DirInternal,B4XPages.GetManager.MainPage.GetFilename(teamLogo)) = False Then
                Wait For (B4XPages.GetManager.MainPage.DownloadAndSave(teamLogo, File.DirInternal, B4XPages.GetManager.MainPage.GetFilename(teamLogo))) Complete (success As Boolean)
                If success Then
                    Log($"${B4XPages.GetManager.MainPage.GetFilename(teamLogo)} geladen und gespeichert..."$)
                End If
            End If
            Dim homeimg As Bitmap
            homeimg.Initialize(File.DirInternal,B4XPages.GetManager.MainPage.GetFilename(teamLogo))
  
            Dim rivalLogo As String = match.Get("rivalLogo")
            If File.Exists(File.DirInternal,B4XPages.GetManager.MainPage.GetFilename(rivalLogo)) = False Then
                Wait For (B4XPages.GetManager.MainPage.DownloadAndSave(rivalLogo, File.DirInternal, B4XPages.GetManager.MainPage.GetFilename(rivalLogo))) Complete (success As Boolean)
                If success Then
                    Log($"${B4XPages.GetManager.MainPage.GetFilename(rivalLogo)} geladen und gespeichert..."$)
                End If
            End If
            Dim rivalimg As Bitmap
            rivalimg.Initialize(File.DirInternal,B4XPages.GetManager.MainPage.GetFilename(rivalLogo))
  
            Dim startDate As String = match.Get("startDate")
  
            'Dim dateOfAvailability As String = colj2Root.Get("dateOfAvailability")
            'Dim dateOfVisibility As String = colj2Root.Get("dateOfVisibility")
            DateTime.DateFormat = "yyyy-MM-dd'T'HH:mm:ssX"
            'DateTime.DateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
            'Dim dateAvailable As Long = DateTime.DateParse(startDate)
            'Dim dateVisible As Long = DateTime.DateParse(B4XPages.GetManager.MainPage.RemoveMilliseconds(dateOfVisibility))
            DateTime.DateFormat = "yyyy-MM-dd'T'HH:mm"
            Dim EventDate As Long = DateTime.DateParse(startDate)
            DateTime.DateFormat = "dd.MM.yyyy HH:mm"
            'Log($"Event-ID =${eventId} Available $Date{dateAvailable}"$)
            'Log($"Event-ID =${eventId} Visible   $Date{dateVisible}"$)
            Log($"Event-ID =${eventId} Match ${match}"$)
            Log($"Event-ID =${eventId} eventName ${eventName}"$)
            Log($"Event-ID =${eventId} eventdate ${EventDate}"$)
            Log($"Event-ID =${eventId} Startdate $Date{EventDate}"$)
            Log($"Event-ID =${eventId} TeamLogo ${teamLogo}"$)
            Log($"Event-ID =${eventId} RivalLogo ${rivalLogo}"$)
            'event.EventDate
            Dim event As HuskiesEvent = B4XPages.GetManager.MainPage.CreateHuskiesEvent(eventId, eventName,EventDate,homeimg,rivalimg,HomeTeam,RivalTeam)
            elist.Add(event)
  
            DateTime.DateFormat = "dd.MM.yyyy"
            'Dim DatumHeute As String = DateTime.Date(DateTime.Now)
            Dim Datum As String = DateTime.Date(event.EventDate)
            'If Datum = DatumHeute Then
            Eventsheute = Eventsheute +1
            Dim p As B4XView = xui.CreatePanel("")
            p.SetLayoutAnimated(0,0,0,100%x,200dip)
            p.LoadLayout("Event")
            clv.InsertAt(0,p,200dip,event)
  
            DateTime.DateFormat = "dd.MM.yyyy HH:mm"
            Dim Datum As String = DateTime.Date(event.EventDate)
  
            lblDatum.Text = Datum
            lblName.Text = eventName
            ivHome.SetBitmap(homeimg)
            ivRival.SetBitmap(rivalimg)
  
            'End If
  
        Next
        elist.SortType("EventDate",True)
        If Eventsheute=0 Then
            Dim dummy As HuskiesEvent
            Dim p As B4XView = xui.CreatePanel("")
            p.SetLayoutAnimated(0,0,0,100%x,200dip)
            p.LoadLayout("Event")
            clv.InsertAt(0,p,200dip,dummy)
  
            lblDatum.Text = "--.--.----"
            lblName.Text = "HEUTE KEIN SPIEL"
            ivHome.Visible = False ' .SetBitmap(homeimg)
            ivRival.Visible = False ' .SetBitmap(rivalimg)
  
        End If
  
        Log(result)
  
  
  
        'Dim jp As JSONParser
        'jp.Initialize(result)
        'Dim jRoot As Map = jp.NextObject
    Else
        Log($"JobError: ${job2.ErrorMessage}"$)
    End If
    job2.Release

End Sub

The error i get does not give me any useful info where exactly the error happens. Guess in b4xpage-code as it is mentioning JavaObject


Do someone have an idea what could be wrong in my implementation? Any hint is highly appreciated.

 
Last edited:

Peter Simpson

Expert
Licensed User
Longtime User
I know that you've seen this line Manfred, 'Object should first be initialized (JavaObject).', but what happens when you single step a line at a time through your code?

What exactly does it crash?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You can tell where by looking for Java line 77 in the class code for ResumableSub_B4XPage_Appear in the code under Objects/src/...
This is line 77 in Eventspage in sub b4xpage_appear
B4X:
    B4XPages.GetManager.ActionBar.RunMethod("setDisplayHomeAsUpEnabled", Array(True))

The code
B4X:
B4XPages.GetManager.ActionBar
returns an JavaObject
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
This is line 77 in Eventspage in sub b4xpage_appear
That's not Java, that's the B4X code. In the generated Java there will be a class called ResumableSub_B4XPage_Appear, It will either be in eventspage.java somewhere under the Ojects/src folder, or more likely from the low line number in a separate file like ResumableSub_B4XPage_Appear.java. It's Java line 77 you want.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
That's not Java, that's the B4X code.
ok.
B4X:
 //BA.debugLineNum = 75;BA.debugLine="B4XPages.GetManager.ActionBar.RunMethod(\"setDispl";
parent._b4xpages._getmanager /*de.huskieskassel.ticketscanner.b4xpagesmanager*/ (ba)._actionbar /*anywheresoftware.b4j.object.JavaObject*/ .RunMethod("setDisplayHomeAsUpEnabled",new Object[]{(Object)(parent.__c.True)}); // LINE 77
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
That's a coincidence that both B4X and Java lines have the same line number. That can't happen too often!

It looks like the JavaObject instance called ActionBar in B4XPagesManager has not been initialised when you reference it. Perhaps you are doing something out of turn. I'm afraid that I have no idea what ActionBar is as I haven't really used B4XPages.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Perhaps you are doing something out of turn.
This is exactly what i am trying to find out.
I can not see anything that i forget to implement. That´s why i create this thread here. ;-)

After an additional compare i now found the mistake. DAMMIT

I did compare the CODE in the Example and in my Project SUB for SUB, line for line. Probably a dozent times...

the problem was inside the - collapsed - Region Activity Attributes
B4X:
#Region  Activity Attributes
    #FullScreen: false
    #IncludeTitle: true
#End Region
IncludeTitle was false
But to use a Actionbar it has to be true.

Thank you for not knowing what a ActionBar is but helping me! ?
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
I just had that the same issue !
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…