Android Question Lost initialization of variables in Starter Service?

Discussion in 'Android Questions' started by FrankBerra, Jun 27, 2019.

  1. FrankBerra

    FrankBerra Active Member Licensed User

    Hello everyone

    My app has service that startes in background from time to time. When i Start the service i immediately start foreground in this way:

    Code:
    Sub Service_Start (StartingIntent As Intent)
       StartTheForeground
       <....all instructions here...> 
       StopTheForeground
    End sub

    Sub StartTheForeground
       
    If IsPaused(Main) Then
           
    Dim n As Notification
           n.Initialize2(n.IMPORTANCE_LOW)
           n.Icon = 
    "notifica_generica"
           n.Light = 
    False
           n.Sound = 
    False
           n.Vibrate = 
    False
           n.SetInfo2(Starter.MappaEtichette.Get(
    "NotificaDiSistema_check_Titolo"), Starter.MappaEtichette.Get("NotificaDiSistema_check_Testo"), "Visualizza_SpiegazioneNotificaGenerica", Main)
           
    Service.StartForeground(100, n)
       
    End If
    End Sub
    The bad thing is that based on the Crashlytics information some random devices (and android versions) crashes with the following errors:

    Code:
    Fatal Exception: java.lang.RuntimeException: Unable to start service project.me.radar@aba4609 with Intent { cmp=project.me/.radar (has extras) }: java.lang.RuntimeException: java.lang.RuntimeException: Object should first be initialized (Map).
           at android.app.ActivityThread.handleServiceArgs + 3744(ActivityThread.java:3744)
           at android.app.ActivityThread.-wrap20(Unknown Source)
           at android.app.ActivityThread$H.handleMessage + 1880(ActivityThread.java:1880)
           at android.os.Handler.dispatchMessage + 106(Handler.java:106)
           at android.os.Looper.loop + 166(Looper.java:166)
           at android.app.ActivityThread.main + 6861(ActivityThread.java:6861)
           at java.lang.reflect.Method.invoke(Method.java)
           at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 450(RuntimeInit.java:450)
           at com.android.internal.os.ZygoteInit.main + 936(ZygoteInit.java:936)



    Caused by java.lang.RuntimeException: java.lang.RuntimeException: Object should first be initialized (Map).
           at anywheresoftware.b4a.BA.raiseEvent2 + 233(BA.java:233)
           at anywheresoftware.b4a.BA.raiseEvent + 176(BA.java:176)
           at project.me.radar.handleStart + 100(radar.java:100)
           at project.me.radar.access$000 + 8(radar.java:8)
           at project.me.radar$1.run + 71(radar.java:71)
           at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand + 235(ServiceHelper.java:235)
           at project.me.radar.onStartCommand + 69(radar.java:69)
           at android.app.ActivityThread.handleServiceArgs + 3718(ActivityThread.java:3718)
           at android.app.ActivityThread.-wrap20(Unknown Source)
           at android.app.ActivityThread$H.handleMessage + 1880(ActivityThread.java:1880)
           at android.os.Handler.dispatchMessage + 106(Handler.java:106)
           at android.os.Looper.loop + 166(Looper.java:166)
           at android.app.ActivityThread.main + 6861(ActivityThread.java:6861)
           at java.lang.reflect.Method.invoke(Method.java)
           at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 450(RuntimeInit.java:450)
           at com.android.internal.os.ZygoteInit.main + 936(ZygoteInit.java:936)



    Caused by java.lang.RuntimeException: Object should first be initialized (Map).
           at anywheresoftware.b4a.AbsObjectWrapper.getObject + 50(AbsObjectWrapper.java:50)
           at anywheresoftware.b4a.objects.collections.Map.Get + 64(Map.java:64)
           at project.me.radar._vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2 + 346(radar.java:346)
           at project.me.radar._service_start + 825(radar.java:825)
           at java.lang.reflect.Method.invoke(Method.java)
           at anywheresoftware.b4a.BA.raiseEvent2 + 196(BA.java:196)
           at anywheresoftware.b4a.BA.raiseEvent + 176(BA.java:176)
           at project.me.radar.handleStart + 100(radar.java:100)
           at project.me.radar.access$000 + 8(radar.java:8)
           at project.me.radar$1.run + 71(radar.java:71)
           at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand + 235(ServiceHelper.java:235)
           at project.me.radar.onStartCommand + 69(radar.java:69)
           at android.app.ActivityThread.handleServiceArgs + 3718(ActivityThread.java:3718)
           at android.app.ActivityThread.-wrap20(Unknown Source)
           at android.app.ActivityThread$H.handleMessage + 1880(ActivityThread.java:1880)
           at android.os.Handler.dispatchMessage + 106(Handler.java:106)
           at android.os.Looper.loop + 166(Looper.java:166)
           at android.app.ActivityThread.main + 6861(ActivityThread.java:6861)
           at java.lang.reflect.Method.invoke(Method.java)
           at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 450(RuntimeInit.java:450)
           at com.android.internal.os.ZygoteInit.main + 936(ZygoteInit.java:936)
    where the sub _vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2 is StartTheForeground.

    The only "map" i access from that sub is "Starter.MappaEtichette" that should be already initialized...
    (I can't reproduce the errors on my devices)
    Is there something that i can do for avoiding this kind of crashes?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    How is the service started? Which version of B4A are you using?
     
  3. FrankBerra

    FrankBerra Active Member Licensed User

    The service is starter in this way:
    Code:
    StartServiceAtExact(Radar, DateTime.Now+2*DateTime.TicksPerSecond, True'From the starter service


       
    Dim MomentoDiRestart As Long
       MomentoDiRestart = 
    DateTime.Now + Minutes * DateTime.TicksPerMinute 
       StartServiceAtExact(Me, MomentoDiRestart, 
    True'From service itself
    The version of B4A is 9.01.2
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Why are you using StartServiceAtExact for this? This is not the correct use case for a scheduled service.

    You need to use:
    Code:
    Sleep(2000)
    StartService(Radar)
    StartService(MomentiDiRestart)
     
  5. FrankBerra

    FrankBerra Active Member Licensed User

    Thank you for hint.
    So are you telling me that if restart the service Radar, let say every 20 minutes, with
    StartServiceAtExact(Me, MomentoDiRestart, True)
    i am going to loose the initialization of variables in starter service?
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Scheduling the service to start every 2 seconds with StartServiceAt is a mistake.
    You should use a timer for this.

    Somehow the process was killed and the starter service wasn't able to run.
     
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