Android Question Question about background process


Active Member
Licensed User
I have declared in the main activity at process globals
a list : Dim tempSensors As List
Init. is in activity_create.

I have a Service : BluetoothReceiver where I receive data.
Here I use a Function from another class called CL_CENTRAL
The function I call : translate_code from CL_CENTRAL

Here I access the List "tempSensors" from the main activity process globals.
If Main.tempSensors.IndexOf(strCode.SubString2(0,4) )> -1 Then ....

The app is running for a while without problems, specially in debug mode. In release mode
from time to time, after several hours, I get this error code.

It looks like that the main activity is not initialized anymore and the service still running
I thought process globals are not killed.

How I can solve the problem.

sending message to waiting queue of uninitialized activity (setstate)
cl_central_translate_code (java line: 813)
java.lang.RuntimeException: Object should first be initialized (List).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(
    at anywheresoftware.b4a.objects.collections.List.IndexOf(
    at jk.central433.base.cl_central._translate_code(
    at jk.central433.base.bluetoothreceiver$ResumableSub_ast_NewText.resume(
    at jk.central433.base.bluetoothreceiver._ast_newtext(
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(
    at anywheresoftware.b4a.keywords.Common$
    at android.os.Handler.handleCallback(
    at android.os.Handler.dispatchMessage(
    at android.os.Looper.loop(
    at java.lang.reflect.Method.invoke(Native Method)
** Receiver (bluet

'class: cl_central

public Sub translate_code(strCode As String) As String
    Dim ret As String
    Dim po As Int
    Dim ls_Sql As String
    Dim prfx As String
    Dim cu As Cursor

    Dim flgTemp As Int
    Dim TempCelsius As String

    ret = strCode
    If strCode.Length=6 Then
        If  Main.tempSensors.IndexOf(strCode.SubString2(0,4) )> -1  Then
            TempCelsius= strCode.SubString(strCode.Length-2)
            strCode= strCode.SubString2(0,4)
        End If
    End If
    ls_Sql="select code,description,id_action  from t_code  where code='" & strCode & "'"
    If cu.RowCount=0 Then
        ls_Sql="select description  from t_remote  where remote_codes ='" & strCode & "'"
        If cu.RowCount=0 Then
           Return ret
        End If
        prfx = "RM "
    End If   
    If flgTemp=1 Then
        prfx="TMP "
        ret = prfx & cu.GetString("description") & " " & TempCelsius
        ret = prfx & cu.GetString("description")
    End If
    Return ret
End Sub


Staff member
Licensed User
Important example: Background location tracking

You should have a foreground service.

I guess that in your app the process is killed and then recreated. Activity_Create will not run when that happens so you will get this error.

As a general rule: the only place where you should have public process global variables is in the starter service, and the variables should be initialized in Service_Create.
Upvote 0


Active Member
Licensed User
Ok, I understand. I have one more question. In the main activity I have a timer running every 30min.
I thought it should prevent the main activity to be killed.
But I will try with a sheldued service and move the globals to the starter service.
Upvote 0