Android Question Illegal State Exception (Android 8/8.1/9)

chrjak

Active Member
Licensed User
Longtime User
I am getting some crazy Errors when trying to start timers or start one service. I was not able to replicate the Errors on my devices.

Problems occur here:
main.java
B4X:
 //BA.debugLineNum = 1252;BA.debugLine="waitingtimer.Enabled = True";
_vvvvvvvvvvvv4.setEnabled(anywheresoftware.b4a.keywords.Common.True);

 //BA.debugLineNum = 852;BA.debugLine="clicktimer.Enabled = True";
_vvvvvvvvvvvv3.setEnabled(anywheresoftware.b4a.keywords.Common.True);

gps.java - on the second line
B4X:
public int onStartCommand(final android.content.Intent intent, int flags, int startId) {
        if (ServiceHelper.StarterHelper.onStartCommand(processBA, new Runnable() {
            public void run() {
                handleStart(intent);

Corresponding B4a Code:
B4X:
Sub Panel4_Touch (Action As Int, X As Float, Y As Float)
    Select Action
      Case Activity.ACTION_DOWN
         Panel4.Color = Colors.RGB(213,51,0)
         Panel4.Elevation = 4dip
         longclicked = False
         clicktimer.Enabled = True
      Case Activity.ACTION_MOVE
         'Log("move")
      Case Activity.ACTION_UP
           clicktimer.Enabled = False
           If longclicked = False Then
         Panel4_Click
         End If
         Panel4.Color = Colors.RGB(255, 87, 34)
         Panel4.Elevation = 4dip
       End Select   
End Sub
Here i had to rebuild click animations because i needed an resizable imageview on a button - crashes are happening on the clicktimer.enabled lines

and at the other part i have only an animation timer that is being activated.

Every timer and this gps service is ONLY started when the application is running in foreground.
All timers are initialized in Activity_Create (every time, not only on FirstTime) after initializing the layout but before everything else
Service is started from a Code Module Sub which is called in Main at some point

IllegalState means Problems between threads and services. Is there any possibility for me to fix this or is it b4a internally?

targetsdk is 28 - i can't remember having these errors before putting it to 26+

thanks for answers
 

chrjak

Active Member
Licensed User
Longtime User
Thanks for the quick reply.

I attach the one of panel4 and the one with the gps service. if more is needed please tell me

java.lang.IllegalStateException:

at anywheresoftware.b4a.objects.Timer.setEnabled (Timer.java:79)

at com.my.app.main._panel4_touch (main.java:2495)

at java.lang.reflect.Method.invoke (Native Method)

at anywheresoftware.b4a.BA.raiseEvent2 (BA.java:191)

at anywheresoftware.b4a.BA$1.run (BA.java:330)

at android.os.Handler.handleCallback (Handler.java:907)

at android.os.Handler.dispatchMessage (Handler.java:105)

at android.os.Looper.loop (Looper.java:216)

at android.app.ActivityThread.main (ActivityThread.java:7625)

at java.lang.reflect.Method.invoke (Native Method)

at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:524)

at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:987)

java.lang.RuntimeException:

at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:4179)

at android.app.ActivityThread.-wrap21 (Unknown Source)

at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2119)

at android.os.Handler.dispatchMessage (Handler.java:108)

at android.os.Looper.loop (Looper.java:166)

at android.app.ActivityThread.main (ActivityThread.java:7529)

at java.lang.reflect.Method.invoke (Native Method)

at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:245)

at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:921)
Caused by: java.lang.IllegalStateException:

at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1701)

at android.app.ContextImpl.startService (ContextImpl.java:1657)

at android.content.ContextWrapper.startService (ContextWrapper.java:644)

at anywheresoftware.b4a.keywords.Common.StartService (Common.java:884)

at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand (ServiceHelper.java:211)

at com.my.app.gps.onStartCommand (gps.java:69)

at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:4151)

I just saw that in my console a new IllegalState Error at main.onCreate was reported. I have 5 Crash Cluster with this Exception
 
Upvote 0

chrjak

Active Member
Licensed User
Longtime User
B4a Version 8.3

The Service starts from a Code Module accessed from the Activity when clicking a button:
B4X:
If GPS.ServiceActive = False Then
        Dim gps2 As GPS
        gps2.Initialize("GPS2")
        If gps2.GPSEnabled = False Then
            Dim result As Int = Msgbox2(Texts(20), "", Texts(21), Texts(22), "", Null)
            If result = DialogResponse.POSITIVE Then
                   StartActivity(gps2.LocationSettingsIntent)               
                Return 1
            Else
                Return 2
            End If
        Else
               If GPS.ServiceActive = False Then StartService(GPS)
            Return 0
           End If
    Else
        Return 0
    End If

The second If wouldn't be neccessary but probably isn't making the trouble
 
Upvote 0
Top