Android Question CustomerListView throwing error

Nitin Joshi

Active Member
Licensed User
Longtime User
I have made a small project to load hr, min, am/pm using CustomListview. However, B4A is throwing error when B4XRunPage is created. I have uploaded the program as well error lines added below...

What could be the reason?

Logger connected to: OnePlus IN2011
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
#SupportedOrientations attribute must be set to landscape or portrait.
Error occurred on line: 30 (B4XRunPage)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.keywords.Common.CallSubDebug2(Common.java:1087)
at B4X.test.b4xpagesmanager._createpageifneeded(b4xpagesmanager.java:1070)
at B4X.test.b4xpagesmanager._showpage(b4xpagesmanager.java:427)
at B4X.test.b4xpagesmanager._addpage(b4xpagesmanager.java:247)
at B4X.test.b4xpagesmanager._addpageandcreate(b4xpagesmanager.java:261)
at B4X.test.b4xpagesmanager._initialize(b4xpagesmanager.java:167)
at B4X.test.main._activity_create(main.java:419)
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 B4X.test.main.afterFirstLayout(main.java:105)
at B4X.test.main.access$000(main.java:17)
at B4X.test.main$WaitForLayout.run(main.java:83)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:240)
at android.os.Looper.loop(Looper.java:351)
at android.app.ActivityThread.main(ActivityThread.java:8423)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:584)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:336)
at anywheresoftware.b4a.debug.Debug.CallSubNew2(Debug.java:285)
... 25 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:318)
... 26 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.keywords.Common.CallSubDebug2(Common.java:1087)
at B4X.test.b4xpagesmanager._createpageifneeded(b4xpagesmanager.java:1070)
at B4X.test.b4xpagesmanager._addpageandcreate(b4xpagesmanager.java:264)
at B4X.test.b4xpages._addpageandcreate(b4xpages.java:57)
at B4X.test.b4xmainpage._b4xpage_created(b4xmainpage.java:84)
... 28 more
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:336)
at anywheresoftware.b4a.debug.Debug.CallSubNew2(Debug.java:285)
... 34 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:318)
... 35 more
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int anywheresoftware.b4a.objects.collections.List.getSize()' on a null object reference
at b4a.example3.customlistview._addtextitem(customlistview.java:78)
at B4X.test.b4xrunpage._b4xpage_created(b4xrunpage.java:98)
... 37 more
** Activity (main) Resume **
 

Attachments

  • CustomListView.zip
    113 KB · Views: 98
Solution
In a B4XPages project we must use a landscape or portrait orientation, because when it is undefined, whenever the screen is rotated, the layout is reloaded from the beginning.

the problem is related to your attempt to add the item to the clv, before the layout is loaded.
1741261572616.png



suggestion, remove LoadLayout from the B4XPage_Appear sub and place it in B4XPage_Created before adding items to the clv
1741261016234.png
1741261132579.png



If you always need to reload the layout when the screen is opened, it would be better to pass your code to B4XPage_Appear
1741261317471.png

DonManfred

Expert
Licensed User
Longtime User
The error is clearly stated

#SupportedOrientations attribute must be set to landscape or portrait.

Set the #SupportedOrientations to either landscape or portrait

Change
B4X:
#SupportedOrientations: unspecified

to
B4X:
#SupportedOrientations: landscape
or
B4X:
#SupportedOrientations: portrait

B4XPages are limited to one Orientation. It must be set in Main in the above attribute.

See

Before we start:

1. You are not forced to use B4XPages. All the current features behave exactly as before.
2. It does have some limitations. One notable limitation is that in B4A, the activity that holds all the pages should be locked to a single orientation.
 
Last edited:
Upvote 0

PaulMeuris

Well-Known Member
Licensed User
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int anywheresoftware.b4a.objects.collections.List.getSize()' on a null object reference
This is the real cause of the error.
The custom listviews are unknown until the layout is loaded.
In B4XRunPage module::
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("Run_Page")
    'Log("RunPage Created")
    For i = 1 To 12
        OnHrLst.AddTextItem(NumberFormat(i,2,0),i)
    Next
It's not recommended to use both orientations.
 
Upvote 0

Nitin Joshi

Active Member
Licensed User
Longtime User
B4XPages are limited to one Orientation. It must be set in Main in the above attribute.
@DonManfred Project was working well earlier. This error floated after CustomListView added to my project.
This is the real cause of the error.
The custom listviews are unknown until the layout is loaded.
@PaulMeuris : I have created one more sample project with only one B4XPage (B4XMainPage). CustomListView can add items.

So now i am confused whether issue is due to orientation or something else. I will check tonight by changing orientation. If orientation is the issue then is it only for CustomListView because (As stated above) error started to float after CustomListView control is added.

Expert opinion please... 🙏
 
Upvote 0

Nitin Joshi

Active Member
Licensed User
Longtime User
Dear All, Please allow me to elaborate my point...
I am aware that B4A does not support orientation while using B4XPages. However, even if we keep orientation unsepcified, APP can run - can open in mobile. If user changes the orientation then APP will be shut down (will run perfect if orientation is kept same)
After additing CustomListView, APP can not start i. e. can not load any layout as error throws in page creation event.
 
Upvote 0

Nitin Joshi

Active Member
Licensed User
Longtime User
Get your backup before adding the clv and compile it, run it and switch orientation. The app will crash like it does now.
Yes, fully agree and exactly happening like stated. However when CLV is added, APP crashes instantly as error floats inside Page creation event.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
However when CLV is added, APP crashes instantly as error floats inside Page creation event.
The solution is still in #2.

Set the #SupportedOrientations to either landscape or portrait and it will work.
As written it is a limitation of B4XPages.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Have you really carefully read post #3 !?
PaulMeuris gave you the explanation and the solution why don't you apply it ?

There are quite some mistakes in your code.
It took me a while to find out what happened in your code.

1. To zip a B4XPages project you should click on the line below in the B4XMainPage module.
B4X:
'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip
With your zip file we get an error that ROBOCOPY cannot access the SharedFiles folder.
I commented the line but still the same error, then i saw that you also have the same lines on top of the B4XRunPage module.
It seems that you copied the code from the B4XMainPage module, why ?

2. Orientation as already explained.

3. When you create B4XRunPage you do not load the layout as PaulMeuris already explained in post #3.
You must load the layout in the B4XPage_Created routine in the B4XRunPage module as shown in post#3.
In your case, you try to access a view which is unknown by the compiler, therefore the crash and error message !

4. After adding the load of the layout, no crash, but only an empty screen when showing B4XRunPage.
Looking deeper i saw that the visibility of pnlMain in the Run_Page layout is set to False. So, normal that nothing is displayed.

5. Visibility of pnlMain set to True. Still an empty screen.
Then i saw that the roomPanel has an elevation of 4 but pnlMain has an elevation of 0.
Therefore pnlMain is behind roomPanel and nothing is seen !

6. Then, set roomPanel elevation to 0 and now roomPanel is visble. But the CustomListViews are not filled, why ?
Then i saw that in the B4XPage_Appear routine in the B4XRunPage module you remove all views and you reload the layout, why ?
And of course, after commenting the two lines, the CustomListViews remain filled !
You have the same in the B4XMainPage module you remove all views and load the layout again, why ?

I am convinced that your attempt to setup times with several CustumListViews is not a good solution.
Scrolling over 60 items to select minutes will be cumbersom, even filling an EditText view with the keyboard would be more efficient.

It is really annoying and frustrating when helpers need to spend a lot of time to go through all those steps before getting a result.
If your goal was to test the ability of helpers, this is a very good example.
 
Upvote 0

Nitin Joshi

Active Member
Licensed User
Longtime User
If your goal was to test the ability of helpers,
@klaus, Please do not take me wrong. It was not my goal. I think many times before posting any thread. After that i am sorry as you had to put time and efforts to investigate.

I am not a professional APP developer (I do it as hobby. I give time for coding on weekend or when back home from work). I am still learner even though i am a long time user. In fact, its less than 2 months i purchased B4i license and started to develop cross platform coding...

I really appreciate your indepth knowledge, careful analysis and professionalism.

Next time, i will take more care before putting any post. This is a good learning for me, thank you. 🙏🙏
 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime User
In a B4XPages project we must use a landscape or portrait orientation, because when it is undefined, whenever the screen is rotated, the layout is reloaded from the beginning.

the problem is related to your attempt to add the item to the clv, before the layout is loaded.
1741261572616.png



suggestion, remove LoadLayout from the B4XPage_Appear sub and place it in B4XPage_Created before adding items to the clv
1741261016234.png
1741261132579.png



If you always need to reload the layout when the screen is opened, it would be better to pass your code to B4XPage_Appear
1741261317471.png
 
Upvote 0
Solution

Nitin Joshi

Active Member
Licensed User
Longtime User
@PaulMeuris @DonManfred @klaus @Lucas Siqueira Thank you very much 🙏 . All of you have given valuable suggestions.

Conclusion: To add items in CLV, must load layout first. We can keep orientation unspecified. APP can run, open. It will crash if user switches the orientation.

By the way, I am switching to B4XPlusMinus control to set time. I found it simple and flawlessly works for B4A and B4i (codes are compatible)
 
Upvote 0
Top