Android Question Lost initialization of variables in Starter Service?

FrankBerra

Active Member
Licensed User
Longtime 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:

B4X:
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:

B4X:
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?
 

FrankBerra

Active Member
Licensed User
Longtime User
The service is starter in this way:
B4X:
   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
 
Upvote 0

FrankBerra

Active Member
Licensed User
Longtime 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?
 
Upvote 0
Top