Android Question How to make SQL object reusable

Discussion in 'Android Questions' started by SimonCarr, Aug 16, 2019.

Tags:
  1. SimonCarr

    SimonCarr Member Licensed User

    I am using SQLITE with B4A. I am using the database in multiple activities. I have created a service to contain my database object. StartAtBoot is set to True.

    sqlManager
    Code:
    #Region  Service Attributes
    #StartAtBoot: True
        
    #End Region

    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.
        Public sql As SQL
    End Sub

    Sub Service_Create
        
    End Sub

    Sub Service_Start (StartingIntent As Intent)
        
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
        sql.Initialize(File.DirInternal,"ionpwsql",True)
    End Sub

    Sub Service_Destroy

    End Sub
    In my main module, I also try to start the service in my Activity_Create method

    Main
    Code:
    Sub Activity_Create(FirstTime As Boolean)
        
    'Do not forget to load the layout file created with the visual designer. For example:
        
        
    Dim tabHostExtra As TabHostExtras
        
    StartService(sqlManager)
        
    If DBUtils.TableExists(sqlManager.sql, "category") = False Then
            CreateTables
        
    End If
        
    Activity.LoadLayout("main")
        appTabs.AddTab(
    "Generate""createPassword")
        appTabs.AddTab(
    "Passwords""passwordList")
        appTabs.AddTab(
    "Settings""settings")
        tabHostExtra.setTabTextSize(appTabs, 
    8.0)
        tabHostExtra.setTabHeight(appTabs, 
    50)
        
        
    For i = 1 To 20
            
    Dim pd As PasswordData
            pd.Initialize
            pd.Category = 
    "Category " & i
            pd.Name = 
    "Name " & i
            CustomListView1.Add(CreateItem(pd), pd)
        
    Next
        
    End Sub

    But when I try to run the application, I get the following error saying the Object should first be initialized.

    Code:
    *** Service (starter) Create ***
    ** 
    Service (starter) Start **
    ** 
    Activity (main) Create, isFirst = true **
    Error occurred on line: 
    487 (DBUtils)
    java.lang.RuntimeException: Object should first be initialized.
        at anywheresoftware.b4a.sql.SQL.checkNull(
    SQL.java:55)
        at anywheresoftware.b4a.sql.SQL.ExecQuerySingleResult2(
    SQL.java:244)
        at b4a.example.dbutils._tableexists(dbutils.java:
    38)
        at b4a.example.main._activity_create(main.java:
    484)
        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:
    144)
        at b4a.example.main.afterFirstLayout(main.java:
    104)
        at b4a.example.main.access$
    000(main.java:17)
        at b4a.example.main$WaitForLayout.run(main.java:
    82)
        at android.os.Handler.handleCallback(Handler.java:
    873)
        at android.os.Handler.dispatchMessage(Handler.java:
    99)
        at android.os.Looper.loop(Looper.java:
    193)
        at android.app.ActivityThread.main(ActivityThread.java:
    6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:
    493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    858)
    ** 
    Activity (main) Resume **
     
  2. OliverA

    OliverA Expert Licensed User

    Use the Starter service to I initialize your sql object
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    And initialize it in Service_Create. Service_Start can be called multiple times.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice