I have error issues on resume

Penko

Active Member
Licensed User
Longtime User
Hello :)

I experience error issues randomly when resuming my application. So far I have understood that they happen when I resume my application after I haven't used the phone for quite some time(with my application not started).
When I then decide to start it, I get these errors. My last opinion is that they are related to the database but I am not sure.

To make it more clear, if I Force Stop my application and start it again, I have no issues with it. Tests do load, everything else as well.

In the event of a failure, none of the Activities that use data from the Database load properly. All of them are returning error messages in Subs that retrieve information from the database.

The latest issue was:
An error has occured in sub: sqll_gettests (java line: 1230).
java.lang.RunTimeException:

Object should be first initialzed (cursor).

This is the beginning of SQLL_GetTests
B4X:
Dim q As String
Dim result As Cursor

Dim theList As List : theList.Initialize


q = "SELECT * FROM tests ORDER BY TEST_ID DESC"
result = ExecuteForResult(q) ' this Simply returns a cursor. I am using it to Log Queries globally.

Common.LogT("SQLL.GetTests() result = " & result, "QUERY_RESULT")

Here is how I initialize the DB:
B4X:
Sub initialize1DB()

Try
   SQL1.Initialize(AppSettings.dbDir, AppSettings.dbName, False)
   
   Common.LogT("initialize1DB() -- DB initialized", "")
   
Catch
   Common.LogT("initialize1DB()" & LastException, "")

End Try


End Sub

Is it possible that the connection to the database is closed? Is there any time period after which this is done natively by Android or by my application as well? I am asking because I had issues with other parts of my application and wrote a Sub that initializes what is necessary for the current Activity. It's called in Resume.

So, if my connection is closed, I will have to just add DB logic to the same Sub.

The problem is this happens randomly, all of a sudden and I can't investigate too much.

Any ideas?
 

Penko

Active Member
Licensed User
Longtime User
Hi, I have no FirstTime-dependent logic.

Here is a part of the Sub that loads Tests(it's much longer and it's not necessary to post the contents below). This sub is called first in Activity_Create without any reference to FirstTime.

B4X:
Sub Activity_Create(FirstTime As Boolean)

   Activity.LoadLayout(main_layout)
 Common.LogT("Activity_Create()  FirstTime = " & FirstTime, "ACTIVITY_EVENT")
   
   SetOnlyOneTime ' this calls the Sub that ads the Menus for the current Activity. I had similar troubles with menus but have not had any recently so I believe they are sorted out because I did some changes to the Sub to initialize if it hasn't.
      painter("") ' here is where we call the Tests Painer.
   setEverything ' this does the remaining work - setTitle, setTexts, setValues, etc...
   
End Sub


Sub painter(operation As String)

Common.LogT("Tests.painter() loading layout...", "")
Common.LogT("Tests.painter() operation = " & operation, "")

If(operation = "RELOAD") Then
   Common.clearPanel(scrollViewMain.Panel) ' we have to clear panels to avoid double addition.
End If

Dim panelC As Int

Dim thedata As List
thedata = SQLL.GetTests ' here is where I call GetTests and where it crashes(sqll_gettests).
 
Last edited:
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Hmm...
Can you identify what is on this line?
B4X:
An error has occured in sub: sqll_gettests (java line: 1230).
java.lang.RunTimeException:

Object should be first initialzed (cursor).
Make sure it is the latest compiled version that showed the problem.
The Main.java is probably in Objects/src/<packagename>/main.java

It does look like a database problem, in that you are trying to access the cursor before it is initialized.
 
Upvote 0

Penko

Active Member
Licensed User
Longtime User
I've included it in the first post but sorry for not making it very clear:

B4X:
Dim q As String
Dim result As Cursor

Dim theList As List : theList.Initialize


q = "SELECT * FROM tests ORDER BY TEST_ID DESC"
result = ExecuteForResult(q) ' this Simply returns a cursor. I am using it to Log Queries globally.

Common.LogT("SQLL.GetTests() result = " & result, "QUERY_RESULT")

This cursor should be initialized because in normal conditions the tests load without an troubles.

That's why I tend to think it is because the connection to the database is somehow closed if the phone is not touched for some period. I now added some logic on Resume to re-initialize database but will be testing if it will work.
 
Upvote 0

timo

Active Member
Licensed User
Longtime User
I don't understand 100% the way you code this db, but I see that the cursor is never positionned. Is it possible to have a 'full code' reduction to test? Do you put somewhere db.close too?
 
Upvote 0

Penko

Active Member
Licensed User
Longtime User
Timo, I just didn't post the entire Sub. Here I do it now:

B4X:
Sub GetTests() As List

Dim q As String
Dim result As Cursor

Dim theList As List : theList.Initialize


q = "SELECT * FROM tests ORDER BY TEST_ID DESC"
result = ExecuteForResult(q)

Common.LogT("SQLL.GetTests() result = " & result, "QUERY_RESULT")

For i = 0 To result.RowCount - 1

   result.Position = i
   
   Dim thisTest As Tests : thisTest.Initialize
   Dim theseLists As List

   ' clear
   'thisTest.Initialize
   'theseLists.Clear

   thisTest.test_id = result.GetInt("TEST_ID")
   
   ' get all lists for the current test
   theseLists = GetConditionList(thisTest.test_id, "USE_LISTS")
   thisTest.list_id = theseLists
   
   thisTest.test = result.GetString("TEST")
   thisTest.notes = result.GetString("NOTES")
   thisTest.MultiChoice = result.GetInt("MULTICHOICE")
   thisTest.SortMode = result.GetInt("SORTMODE")
   thisTest.Status = result.GetInt("STATUS")
   thisTest.progress = Common.GetTestProgress(thisTest.test_id)
   
   Common.LogT("----------------------------------------------------------", "")
   Common.LogT("SQLL.GetTests() thisTest = " & thisTest, "")
   Common.LogT("----------------------------------------------------------", "")
   
   theList.Add(thisTest)
   
   

Next

result.Close

Common.LogT("SQLL.GetTests() theList = " & theList, "")

Return theList

End Sub

To answer your other question, I don't close the database.
The problem is only when I don't use the application for quite some(long period) time.
 
Upvote 0

timo

Active Member
Licensed User
Longtime User
I think something is not correct working on reinitializing the db. In this situation you can first try changing the initialization; put it into main Activity_create, inside a 'If First time...' and without try-catch . What happened once to me is that when the activity which initialized the DB was paused I had such a simililar problem in controlling if a DB was iitilized or not on resume. I provisory solved by adding a KeepAlive/release in every secondary Activity (resume/pause), but not in the main one, which was not 'finished' by code on pause. So, the db 'lived' as long as the app did.
 
Last edited:
Upvote 0
Top