Android Question Widget Issue - Solved

AllyAndroid

Member
Licensed User
Longtime User
I am working on adding a Widget to one of my apps. I followed the tutorial and was able to add the widget and get it working. But if I make any changes to the app and try to reinstall it on my phone, I always get the java.lang.RuntimeException: Unable to create service error. After the error, it appears to go ahead and create the service.

This only happens when I have the widget already on my Home Screen. If I remove the widget and then re-install the app, I do not get the error.

B4X:
java.lang.RuntimeException: Unable to create service com.isotoria.turboflash.flashlight: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2595)
    at android.app.ActivityThread.access$1800(ActivityThread.java:139)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5097)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at com.isotoria.turboflash.flashlight.onCreate(flashlight.java:33)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2585)
    ... 10 more
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.isotoria.turboflash.flashlight.onCreate(flashlight.java:31)
    ... 11 more
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
    at com.isotoria.turboflash.main.initializeProcessGlobals(main.java:616)
    ... 14 more
Caused by: java.lang.NullPointerException
    at com.isotoria.turboflash.main._process_globals(main.java:756)
    at com.isotoria.turboflash.main.initializeProcessGlobals(main.java:607)
    ... 14 more

** Service (flashlight) Create **
** Service (flashlight) Start **
** Activity (main) Create, isFirst = true **

Any suggestions on how to prevent this error from happening? I've tried debugging but it happens before any of my code is run.
 

DonManfred

Expert
Licensed User
Longtime User
It could help if you post some code (the widget-service)
 
Upvote 0

AllyAndroid

Member
Licensed User
Longtime User
It could help if you post some code (the widget-service)

This is all that I have in the Service.

B4X:
#Region Module Attributes
    #StartAtBoot: False
#End Region
'Service module
Sub Process_Globals
    Dim rv As RemoteViews
End Sub
Sub Service_Create
    rv = ConfigureHomeWidget("widget", "rv", 0, "Turbo Flash",True)
End Sub
Sub Service_Start (StartingIntent As Intent)
    If rv.HandleWidgetEvents(StartingIntent) Then Return
End Sub
Sub rv_RequestUpdate
    rv.UpdateWidget
End Sub
Sub rv_Disabled
    StopService("")
End Sub
Sub Service_Destroy

End Sub
Sub ImageView1_Click
    StartActivity("Settings")
End Sub

The widget layout only contains a panel and an image view.
 
Upvote 0

AllyAndroid

Member
Licensed User
Longtime User
Yep, totally misread that one the first time. :oops:

There were a few variables that I was setting default values to in the Sub Process_Globals of my main module.

IE: Public screenIndex As Int: screenIndex = 0

I moved setting my default values from Process_Globals to Activity_Create and that fixed the error on reloading the app.

Follow up question:

If setting the value of a variable in the Process_Globals sub is wrong, why does B4A allow it? Could that possibly be a warning in a future release? Or are there times when it is acceptable to do it?
 
Upvote 0
Top