B4J Question [Solved] B4XPage & CustomListView Problem with re-loading of page

paragkini

Member
Licensed User
Longtime User
Hello All,

I am facing a problem while using B4XPages in B4J.
I have two B4XPages.
Page - 1 : List of topics. Where a topic is selected. After selection of topic and click of action button, a map is populated with questions list and passed to page 2 and page 2 is shown.
Page - 2 : In B4XPage_appear (earlier kept it this in Page_created but after looking for solution on forum, found a thread mentioning to use appear and disappear. Page 2 populates the customlistview basis map passed.

Problem :
When I first time load Page 1, select topic, populate questions map and load Page 2, it works fine.
But when I close Page 2 and come back to Page 1, then select another topic, refresh the map and show Page 2, I get below error on while adding item to customlistview. I tried close_page but didn't work. Currently wrote to clear the custom list view and then reload but still getting the same error.

Screenshots :
0. First page load. Even before adding any item in customlist
1. After first successful load, details of customlistview. and before clearing all the items while closing the page.
2. Even after clearing all the items using CLV.Clear, this shows.
3. When on re-load from Page 1 to Page 2, when trying to add first item.
4. Libraries that I am using
5. B4J version : 10.00

I have tried :
Using solution given in the links :
1. Solution given by Erel in post : How can I reload a B4XPage?
2. Using appear / disappear / clearing CLV before close / clearing each item before closing.

Code Error BEFORE using CreateLayout solution by Erel:
Nullpointer exception while adding first item after re-load:
Error occurred on line: 76 (B4XListQuestions)
java.lang.NullPointerException
    at b4j.example.customlistview._add(customlistview.java:75)

Code Error: After using CreateLayout solution by Erel.
Error after CreateLayout:
Private Sub CreateLayout
    Root.RemoveAllViews
    Root.LoadLayout("loListQuestions")
End Sub

Compiler error after page re-load:
java.lang.RuntimeException: Unexpected command: 0
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:316)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:513)
    at anywheresoftware.b4a.keywords.Common.access$0(Common.java:493)
    at anywheresoftware.b4a.keywords.Common$CallSubDelayedHelper.run(Common.java:567)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:185)
    at java.lang.Thread.run(Thread.java:748)

Nothing worked. Please suggest solution.
 

Attachments

  • 0.png
    0.png
    22.9 KB · Views: 80
  • 1.png
    1.png
    31.4 KB · Views: 88
  • 2.png
    2.png
    30.6 KB · Views: 81
  • 3.png
    3.png
    22.1 KB · Views: 82
  • 4.png
    4.png
    19.4 KB · Views: 86
  • 5.png
    5.png
    42.5 KB · Views: 84
Last edited:

LucaMs

Expert
Licensed User
Longtime User
It's not very clear (or at least I get lost in the images you posted).

I wouldn't use either Appear or Disappear; I would create a Public Sub on the second page, to which I would pass the Map. This Sub would empty the CLV and load it with the Map data.
 
Upvote 0

paragkini

Member
Licensed User
Longtime User
I have uploaded a sample project to recreate the issue. MainPage has 2 buttons. Button 1 is when a map is populated and passed to Page2 and then Page2 is loaded. Works fine. But when I close Page2 and click on Button 1 (with old map) or Button 2 (with new map), it gives below error at line where removeallviews is called.

CreateLayout -Error on re-load Page2:
Private Sub CreateLayout
    Root.RemoveAllViews
    Root.LoadLayout("loPage2")
End Sub

Error logged:
Waiting for debugger to connect...
Program started.
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
Error occurred on line: 29 (pgPage2)
java.lang.RuntimeException: Object should first be initialized (B4XView).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:49)
    at anywheresoftware.b4a.objects.B4XViewWrapper.asPaneWrapper(B4XViewWrapper.java:136)
    at anywheresoftware.b4a.objects.B4XViewWrapper.RemoveAllViews(B4XViewWrapper.java:555)
    at b4j.example.pgpage2._createlayout(pgpage2.java:88)
    at b4j.example.pgpage2._b4xpage_appear(pgpage2.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:513)
    at anywheresoftware.b4a.keywords.Common.access$0(Common.java:493)
    at anywheresoftware.b4a.keywords.Common$CallSubDelayedHelper.run(Common.java:567)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:185)
    at java.lang.Thread.run(Thread.java:748)
 

Attachments

  • B4XPageCLVProblem.zip
    5.9 KB · Views: 86
Upvote 0

PaulMeuris

Active Member
Licensed User
You should clear the clvQuestionList before filling it.
To go over the items in a map it is recommended that you use a For Each structure and the get(key) method.

B4X:
Sub LoadListtoCLV(mapListItems As Map)
    clvQuestionList.Clear
    For Each key In mapListItems.Keys
'    For i = 0 To mapListItems.Size - 1
        Dim ItemInList As CLVQItem
        ItemInList = mapListItems.Get(key)
'        ItemInList = mapListItems.GetValueAt(i)
        ItemInList.IsSelected = False
        clvQuestionList.Add(CreateQuestionListItem(ItemInList), ItemInList)
    Next  
End Sub
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I have some suggestions.

1. Initialize page2 in B4XMainPage_Created
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    page2.Initialize
    B4XPages.AddPage("Page2", page2)
End Sub

2. You don't need to use Root.Remove.AllViews in pgPage2
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("loPage2")
End Sub

'Private Sub CreateLayout
'    Root.RemoveAllViews
'    Root.LoadLayout("loPage2")
'End Sub

3. You can use function signature
B4X:
Public Sub LoadListtoCLV (mapListItems As Map)
So the variable created in MainPage can be pass to Page2 using the parameter.
B4X:
B4XPages.ShowPage("Page2")
page2.LoadListtoCLV(mapTemp)

4. I think you may want to clear clvQuestionList before adding new items in function LoadListtoCLV
B4X:
clvQuestionList.Clear

5. If you don't need the whole item of ItemInList, pass ItemInList.Serial only
B4X:
clvQuestionList.Add(CreateQuestionListItem(ItemInList), ItemInList.Serial)

6. Use List instead of a Map.
B4XMainPage:
Private Sub Button1_Click
    Dim Questions As List
    Questions.Initialize
    For i = 1 To 20
        Dim Item As CLVQItem
        Item.Initialize
        Item.Serial = i
        Item.Title = $"Item number is : A-${i}"$
        Item.Category = $"Item category is A-${i}"$
        Questions.Add(Item)
    Next
    B4XPages.ShowPage("Page2")
    page2.LoadListtoCLV(Questions)
End Sub

pgPage2:
Public Sub LoadListtoCLV (QuestionsList As List)
    clvQuestionList.Clear
    For Each Item As CLVQItem In QuestionsList
        clvQuestionList.Add(CreateQuestionListItem(Item), Item.Serial)
    Next
End Sub
 
Upvote 0

paragkini

Member
Licensed User
Longtime User
Hi Everyone, thanks for your help. I managed to resolve my problem with the help extended.
While I have implemented changes suggested by LucaMs, PaulMeuris for the code changes and Aeric for the detailed step-by-step.

I think I had done two mistakes in my code.
I was re-initializing the page again whenever I wanted to load / re-load the page.
I was using ShowPageAndRemovePreviousPages instead of ShowPage only.

Now the code is working as I expected.
I really appreciate the promptness of the forum to help me.
 
Upvote 0
Top