Android Question B4XPages and xCustomListView - "Object should first be initialized (B4XView)"

philly_tee

Member
Licensed User
Longtime User
Hi - I have moved the CustomListView code from the CLVExample into a new B4XPage class in the same project (as I want the listview in a page that isn't the main page), however I am receiving and error. Where have I gone wrong?

B4XMainPage:
Sub Class_Globals
    Private Root As B4XView
    Public p_UpcomingDates As pgUpcomingDates
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")
    p_UpcomingDates.Initialize
    B4XPages.AddPage("p_UpcomingDates", p_UpcomingDates)
End Sub

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

Private Sub Button1_Click
    'B4XPages.ShowPageAndRemovePreviousPages("UpcomingDates")
    B4XPages.ShowPage("p_UpcomingDates")
    p_UpcomingDates.LoadList
End Sub

pgUpcompingDates is using layouts identical to CLVExample, copied and pasted into this project.
pgUpcomingDates:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private clv1 As CustomListView
    Private clv2 As CustomListView
    Private dd As DDD
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
    dd.Initialize
    xui.RegisterDesignerClass(dd)
    Root.LoadLayout("lyoUpcomingDates")
End Sub

Private Sub CreateListItem(Text As String, Width As Int, Height As Int) As B4XView
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, Width, Height)
    p.LoadLayout("CellItem")
    'Note that we call DDD.CollectViewsData in CellItem designer script. This is required if we want to get views with dd.GetViewByName.
    dd.GetViewByName(p, "Label1").Text = Text
    Return p
End Sub

Public Sub LoadList
    For i = 1 To 20
        clv1.Add(CreateListItem($"Item #${i}"$, clv1.AsView.Width, 60dip), $"Item #${i}"$)
    Next
End Sub

The application loads, and Button 1 on MainPage is visible.

Error Block:
[Application Load]
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
*** mainpage: B4XPage_Appear
** Activity (main) Resume **


[MainPage->Button 1 Click]
*** p_upcomingdates: B4XPage_Created [mainpage]
*** mainpage: B4XPage_Disappear [mainpage]
*** p_upcomingdates: B4XPage_Appear [mainpage]
Error occurred on line: 256 (DDD)
java.lang.RuntimeException: Object should first be initialized (B4XView).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
    at anywheresoftware.b4a.objects.B4XViewWrapper.asLabelWrapper(B4XViewWrapper.java:213)
    at anywheresoftware.b4a.objects.B4XViewWrapper.setText(B4XViewWrapper.java:229)
    at nz.co.tapertech.tvfbstaff.pgupcomingdates._createlistitem(pgupcomingdates.java:114)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:7892)
    at android.widget.TextView.performClick(TextView.java:16219)
    at android.view.View.performClickInternal(View.java:7869)
    at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
    at android.view.View$PerformClick.run(View.java:30880)
    at android.os.Handler.handleCallback(Handler.java:942)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:226)
    at android.os.Looper.loop(Looper.java:313)
    at android.app.ActivityThread.main(ActivityThread.java:8757)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)

Many thanks

Philip
 
Solution
I was just finishing a modified version of your app.
You can make a zip-file from the IDE with File - Export as Zip. This file is only 13 KB large!
In your version i have replaced the library xCustomListView with the XUI Views library (the newer version of the CustomListView).
I modified the layout to take the CLV's from the XUI Views and i generated the members (to the pgUpcomingDates page).
The result looks like this:

Check the syntax of the return statement:
B4X:
Private Sub CreateListItem(Text As String, Width As Int, Height As Int) As B4XView
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, Width, Height)
    p.LoadLayout("CellItem")
    Label1.Text = Text
    Return p.As(B4XView)
End Sub
...

philly_tee

Member
Licensed User
Longtime User
Why do you not call the LoadList subroutine in the B4XPage_Created from the pgUpcompingDates page after you have loaded the layout (Root.LoadLayout("lyoUpcomingDates"))?
Hi Paul - I had it that way originally, with no difference in behaviour:

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    dd.Initialize
    xui.RegisterDesignerClass(dd)
    Root.LoadLayout("lyoUpcomingDates")
    LoadList
End Sub
 
Upvote 0

philly_tee

Member
Licensed User
Longtime User
Sorry - found the problem - rookie mistake. I hadn't noticed that there was designer script in the 'cellitem' layout.
Specifically, when using DDD.GetViewByName, you need to have called DDD.CollectViewsData in your designer script for the CustomListview sub-layout.

Thanks for your time and sanity checking!
 
Upvote 0

PaulMeuris

Active Member
Licensed User
I was just finishing a modified version of your app.
You can make a zip-file from the IDE with File - Export as Zip. This file is only 13 KB large!
In your version i have replaced the library xCustomListView with the XUI Views library (the newer version of the CustomListView).
I modified the layout to take the CLV's from the XUI Views and i generated the members (to the pgUpcomingDates page).
The result looks like this:

Check the syntax of the return statement:
B4X:
Private Sub CreateListItem(Text As String, Width As Int, Height As Int) As B4XView
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, Width, Height)
    p.LoadLayout("CellItem")
    Label1.Text = Text
    Return p.As(B4XView)
End Sub
If you only develop a B4A solution then you can use a Panel object instead.
Happy coding!
 

Attachments

  • TVFB Staff.zip
    13 KB · Views: 147
Upvote 0
Solution

philly_tee

Member
Licensed User
Longtime User

That's awesome - thanks Paul!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…