Android Question Where I have to create a SQL Object?

Jose Cuevas

Member
Licensed User
Longtime User
Hello, I wonder if I can create a single SQL object in a Module Code and access it from others activities, or I have to create a SQL object and initialize it in each Activity?
I ask because after some time of background inactivity, when I return to the App again, and use the SQL object, displays this error message:

An Error has occurred in sub:
invoice_fill_list (java line:498)
java.lang.RuntimeException:
Object should first be initialized.
Continue?

Thanks in advance.
 

LucaMs

Expert
Licensed User
Longtime User
Hello, I wonder if I can create a single SQL object in a Module Code and access it from others activities, or I have to create a SQL object and initialize it in each Activity?
I ask because after some time of background inactivity, when I return to the App again, and use the SQL object, displays this error message:

An Error has occurred in sub:
invoice_fill_list (java line:498)
java.lang.RuntimeException:
Object should first be initialized.
Continue?

Thanks in advance.


You are right, you should "create" a SQL object in a Code Module, in the Process_Globals section and write a Sub to initialize it.
Are you sure that is the SQL object that throws that error?
 
Upvote 0

Jose Cuevas

Member
Licensed User
Longtime User
Hi LucaMs, thanks for your quick response, I actually have two suspects, the principal is the SQL object and the other is a GPS object, which I'm defining in the Process_Globals and initialize it in the Activity_Create in FisrtTime.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Hi LucaMs, thanks for your quick response, I actually have two suspects, the principal is the SQL object and the other is a GPS object, which I'm defining in the Process_Globals and initialize it in the Activity_Create in FisrtTime.

ehm on my phone the GPS is "broken", so, for the moment, i don't use that object.

But set a breakpoint on the "invoice_fill_list" sub, then use F8 (step by step), positioning the cursor on an object variable you can see which is not initialized
 
Upvote 0

Jose Cuevas

Member
Licensed User
Longtime User
I already tried with Debug, but only fails when the App is running stand alone in the Phone. But you give an idea, I will put some msgbox in every step, so I can see wich one is shown before the error message.

Thanks LucaMs.
 
Upvote 0

Jose Cuevas

Member
Licensed User
Longtime User
Confirmed, is the SQL object, I create the Object in the Process_Globals in a Module_Code and Initialized in the Main activity.

I would appreciate any help, thanks in advance.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
If your app is in the background then it may be killed. Later it can be restarted from a different activity or service. In that case the initialization code will not run.

This means that you should check whether the SQL is initialized and if not initialize it from each of the activities.


So we should call a routine in a module that tests and possibly initializes the db from each Activity_Resume, I suppose?!?
I had not expected this. thanks :)
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
You only need to test it in Activity_Create when FirstTime = True.


I had misunderstood an "it", which would made me realize bad. Also, I fear I have already forgotten how Android works exactly!

If the S.O. kills my app and I ran it again, it does not restart from the Main?

Currently, I call a sub ("DBOpenOrCreate", his name is self-explanatory) which is located in a module, regardless of FirstTime.

It is not correct? It could have those problems?
 
Upvote 0

Jose Cuevas

Member
Licensed User
Longtime User
Hi Erel, in fact, I already made the check in the Login activity with this:
B4X:
Sub Activity_Resume
    If Not(CYS.CYSRuteoDB.IsInitialized) Then
        CYS.CYSRuteoDB.Initialize(File.DirDefaultExternal, "CYSRuteo.db", False)
    End If           
End Sub

But show me this error:

An error has occurred in sub:
login_activity_resume (java line:346)
android.database.sqlite.
SQLiteCantOpenDatabaseException: unknown error (code 14):
Could not open database
Continue?

I defined the SQL object in a Code Module called CYS, with this code:
B4X:
Sub Process_Globals
    Dim CYSRuteoDB As SQL
End Sub

And I initialized the object in the Main Activity with this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Splash")
   
    If FirstTime Then   
        If File.Exists(File.DirDefaultExternal, "CYSRuteo.db") Then
          CYS.CYSRuteoDB.Initialize(File.DirDefaultExternal, "CYSRuteo.db", False)
        Else
          CYS.CYSRuteoDB.Initialize(File.DirDefaultExternal, "CYSRuteo.db", True)
          CreateTables
        End If       
    End If

End Sub

Thanks for your help.
 
Upvote 0

Jose Cuevas

Member
Licensed User
Longtime User
I found the problem, in my original code, I assigned File.DirDefaultExternal to a variable called CYS.DBPath, so the code looked like this:
B4X:
Sub Activity_Resume
    If Not(CYS.CYSRuteoDB.IsInitialized) Then
        CYS.CYSRuteoDB.Initialize(CYS.DBPath, "CYSRuteo.db", False)
    End If         
End Sub

Then when Android was killing activity, also killed the contents of the variable and that is why it couldn't reopen the database.

With the code I posted (File.DirDefaultExternal instead CYS.DBPath), everything works fine.

Thanks for your help
 
Upvote 0
Top