Help with understanding ListView initialise error

Nycran

Member
Licensed User
Longtime User
Hi guys, I have an odd problem that I'm not sure how to fix and I'm hoping someone here can shed some light on it.

I have a simple activity with a listview (ListView1). The ListView was created using the designer.

In the Globals section I have:

Dim ListView1 As ListView

My Activity_Create looks like this:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("XList")
   PanelHeading.LoadLayout("HeadingButton")
End Sub

And my resume like this:

B4X:
Sub Activity_Resume
   LoadList
End Sub

It is the LoadList method that loads the data into the XList.

This works fine, except sometimes when I leave the app and then return back to it after an arbitrary period of time, I get an error saying that ListView1 is not initialised. I assume that Android has decided that it needs to free up memory and has nuked the ListView control.

I realise I can call the Initialize method on the listview like so:

ListView1.Initialize("ListView1")

But I don't understand:

* Why does the error occur in the first place? If my Activity_Create method calls

Activity.LoadLayout("XList")

which I assume initializes the ListView, why isn't it being initialized when I return to the app?

* In what event should I put:

ListView1.Initialize("ListView1")

In create? In resume?

* If I need to do this for listviews, what about other controls like buttons, panels, etc?

* What is best practice of initializing objects. Is it not safe to rely on the designer and LoadActivity to initialize objects?

Any help would be appreciated.
 
Last edited:

Nycran

Member
Licensed User
Longtime User
Thanks very much Erel, I appreciate your help.

Here's an archive with the full project files:

http://simbtemp.s3.amazonaws.com/BillBot.zip

There's a fair bit of code in there so apologies if it's a bit of a mess. The offending screen where the error occurs is "XList".

The frustrating thing about the error is that it's hard to reproduce. It only happens after you leave the app (previously being on the XList screen, e.g. Client list) and a period of time has elapsed before you come back. If I could more reliably cause the error then it would be easy to tinker with it until the issue is fixed.

If you actually run the app, it will ask you to create an account of use an existing one. Feel free to login with my account and perform the cloud sync so you have some data in the database to populate the lists. I will PM you the login details.

Happy to pay you to help me find the source of the error btw.. I feel once I know what's causing the error I will be in a stronger position to move forward.
 
Upvote 0
Top