Android Question Widget will not start

William Hunter

Active Member
Licensed User
Longtime User
I am trying to create a widget with three ImageView buttons, and also an ImageView and label to monitor battery level. I can compile and install to a device, but the widget will not start. The Main module is free of code. Code for the Service module is shown below. The errors logged are also shown below.

I don’t understand the nature of the error code and would appreciate an explanation. Also, do I have code in my Service module that is not appropriate for a widget, and should be moved to the Main module? Any help greatly is appreciated.

Regards

Service Module
B4X:
#Region Module Attributes
    #StartAtBoot: True
#End Region

'Service module for Widget´s
Sub Process_Globals
    Dim rv As RemoteViews
    Dim ph As PhoneEvents
    Dim admin As BluetoothAdmin
    Dim GPS1 As GPS
    Dim WifiOffFlag As Boolean : WifiOffFlag = False
    Dim BTOffFlag As Boolean : BTOffFlag = False
    Dim GpsOffFlag As Boolean : GpsOffFlag = False
End Sub

Sub Service_Create
    rv = ConfigureHomeWidget("multiwidget.bal", "rv", 0, "MultiWidget",True)
    ConfigureButtons
    ph.Initialize("Bt")
End Sub

Sub Service_Start (StartingIntent As Intent)
    If rv.HandleWidgetEvents(StartingIntent) Then Return
    StartServiceAt("",DateTime.Now + 900 * 1000,True)'every 15 mins
End Sub

Sub rv_RequestUpdate
    rv.UpdateWidget
End Sub

Sub rv_Disabled
    ph.StopListening
    Dim ph As PhoneEvents
    StopService("")
End Sub

Sub Service_Destroy

End Sub

Sub ConfigureButtons
    ' configure button images
    Dim jhw As JhWifi
    ' Initialize wifi
    jhw.Initialize()
    If jhw.Enabled = False Then 
        rv.SetImage("ImgViewWIFI",LoadBitmap(File.DirAssets,"WIFIoff.png"))
        WifiOffFlag = True
    Else
        rv.SetImage("ImgViewWIFI",LoadBitmap(File.DirAssets,"WIFIon.png"))
        WifiOffFlag = False
    End If
    admin.Initialize("Admin")
    If admin.IsEnabled = False Then
        rv.SetImage("ImgViewBtooth",LoadBitmap(File.DirAssets,"BToff.png"))
        BTOffFlag = True
    Else
        rv.SetImage("ImgViewBtooth",LoadBitmap(File.DirAssets,"BTon.png"))
        BTOffFlag = False
    End If
    GPS1.Initialize("GPS")
    If GPS1.GPSEnabled = False Then
        rv.SetImage("ImgViewGPS",LoadBitmap(File.DirAssets,"GPSoff.png"))
        GpsOffFlag = True
    Else
        rv.SetImage("ImgViewGPS",LoadBitmap(File.DirAssets,"GPSon.png"))
        GpsOffFlag = False
    End If
    Return
End Sub

Sub Bt_BatteryChanged (Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent)
    ' battery level changed
    If Level = 100 Then
        If Plugged = True Then 
            rv.SetImage("ImageView1",LoadBitmap(File.DirAssets,"100b.png"))
        Else  
            rv.SetImage("ImageView1",LoadBitmap(File.DirAssets,"100.png"))
        End If
    Else If Level >= 75  AND Level < 100 Then 
        If Plugged = True Then 
            rv.SetImage("ImageView1",LoadBitmap(File.DirAssets,"80b.png"))
        Else
            rv.SetImage("ImageView1",LoadBitmap(File.DirAssets,"80.png"))
        End If
    Else If Level >= 50  AND Level < 75 Then 
        If Plugged = True Then 
            rv.SetImage("ImageView1",LoadBitmap(File.DirAssets,"60b.png"))
        Else
            rv.SetImage("ImageView1",LoadBitmap(File.DirAssets,"60.png"))
        End If
    Else If Level >= 25  AND Level < 50 Then 
        If Plugged = True Then 
            rv.SetImage("ImageView1",LoadBitmap(File.DirAssets,"40b.png"))
        Else
            rv.SetImage("ImageView1",LoadBitmap(File.DirAssets,"40.png"))
        End If
    Else 
        rv.SetImage("ImageView1",LoadBitmap(File.DirAssets,"e.png"))
    End If    
    rv.SetText("label1",Level&" %")
    rv.UpdateWidget
End Sub

Sub ImgViewWIFI_Click
    Dim jhw As JhWifi
    ' Initialize wifi
    jhw.Initialize()
    If WifiOffFlag = True Then
        rv.SetImage("ImgViewWIFI",LoadBitmap(File.DirAssets,"WIFIon.png"))
        WifiOffFlag = False
        jhw.Enabled = True
    Else
        rv.SetImage("ImgViewWIFI",LoadBitmap(File.DirAssets,"WIFIoff.png"))
        WifiOffFlag = True
        jhw.Enabled = False
    End If
End Sub

Sub ImgViewBtooth_Click
    If BTOffFlag = True Then
        rv.SetImage("ImgViewBtooth",LoadBitmap(File.DirAssets,"BTon.png"))
        BTOffFlag = False
        admin.Enable
    Else
        rv.SetImage("ImgViewBtooth",LoadBitmap(File.DirAssets,"BToff.png"))
        BTOffFlag = True
        admin.Disable
    End If
End Sub

Sub ImgViewGPS_Click
    Dim turnOn As TurnGPS
    turnOn.Initialize()
    If GpsOffFlag = True Then
        rv.SetImage("ImgViewGPS",LoadBitmap(File.DirAssets,"GPSon.png"))
        GpsOffFlag = False
        turnOn.turnGPSOn
    Else
        rv.SetImage("ImgViewGPS",LoadBitmap(File.DirAssets,"GPSoff.png"))
        GpsOffFlag = True
        turnOn.turnGPSOff
    End If
End Sub

Log
B4X:
LogCat connected to: emulator-5554
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
** Service (service) Create **
service_service_create (java line: 317)
java.lang.RuntimeException: java.lang.NoSuchFieldException: service_imgviewwifi
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.getIdForView(RemoteViewsWrapper.java:95)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.loadLayoutHelper(RemoteViewsWrapper.java:83)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.loadLayoutHelper(RemoteViewsWrapper.java:77)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.createRemoteViews(RemoteViewsWrapper.java:58)
    at WH.app.MultiWidget.service._service_create(service.java:317)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:154)
    at WH.app.MultiWidget.service.onCreate(service.java:45)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2959)
    at android.app.ActivityThread.access$3300(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2087)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchFieldException: service_imgviewwifi
    at java.lang.ClassCache.findFieldByName(ClassCache.java:510)
    at java.lang.Class.getField(Class.java:881)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.getIdForView(RemoteViewsWrapper.java:93)
    ... 20 more
java.lang.RuntimeException: Unable to create service WH.app.MultiWidget.service: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NoSuchFieldException: service_imgviewwifi
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2969)
    at android.app.ActivityThread.access$3300(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2087)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NoSuchFieldException: service_imgviewwifi
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:154)
    at WH.app.MultiWidget.service.onCreate(service.java:45)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2959)
    ... 10 more
Caused by: java.lang.RuntimeException: java.lang.NoSuchFieldException: service_imgviewwifi
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.getIdForView(RemoteViewsWrapper.java:95)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.loadLayoutHelper(RemoteViewsWrapper.java:83)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.loadLayoutHelper(RemoteViewsWrapper.java:77)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.createRemoteViews(RemoteViewsWrapper.java:58)
    at WH.app.MultiWidget.service._service_create(service.java:317)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
    ... 13 more
Caused by: java.lang.NoSuchFieldException: service_imgviewwifi
    at java.lang.ClassCache.findFieldByName(ClassCache.java:510)
    at java.lang.Class.getField(Class.java:881)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.getIdForView(RemoteViewsWrapper.java:93)
    ... 20 more
** Service (service) Create **
service_service_create (java line: 317)
java.lang.RuntimeException: java.lang.NoSuchFieldException: service_imgviewwifi
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.getIdForView(RemoteViewsWrapper.java:95)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.loadLayoutHelper(RemoteViewsWrapper.java:83)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.loadLayoutHelper(RemoteViewsWrapper.java:77)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.createRemoteViews(RemoteViewsWrapper.java:58)
    at WH.app.MultiWidget.service._service_create(service.java:317)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:154)
    at WH.app.MultiWidget.service.onCreate(service.java:45)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2959)
    at android.app.ActivityThread.access$3300(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2087)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchFieldException: service_imgviewwifi
    at java.lang.ClassCache.findFieldByName(ClassCache.java:510)
    at java.lang.Class.getField(Class.java:881)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.getIdForView(RemoteViewsWrapper.java:93)
    ... 20 more
java.lang.RuntimeException: Unable to create service WH.app.MultiWidget.service: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NoSuchFieldException: service_imgviewwifi
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2969)
    at android.app.ActivityThread.access$3300(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2087)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NoSuchFieldException: service_imgviewwifi
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:154)
    at WH.app.MultiWidget.service.onCreate(service.java:45)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2959)
    ... 10 more
Caused by: java.lang.RuntimeException: java.lang.NoSuchFieldException: service_imgviewwifi
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.getIdForView(RemoteViewsWrapper.java:95)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.loadLayoutHelper(RemoteViewsWrapper.java:83)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.loadLayoutHelper(RemoteViewsWrapper.java:77)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.createRemoteViews(RemoteViewsWrapper.java:58)
    at WH.app.MultiWidget.service._service_create(service.java:317)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
    ... 13 more
Caused by: java.lang.NoSuchFieldException: service_imgviewwifi
    at java.lang.ClassCache.findFieldByName(ClassCache.java:510)
    at java.lang.Class.getField(Class.java:881)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.getIdForView(RemoteViewsWrapper.java:93)
    ... 20 more
 

William Hunter

Active Member
Licensed User
Longtime User
The errors are related to FILE NOT FOUND. Check your pictures.

Thank you for your reply NJDude. The images are in the Files directory (DirAssets). I have attached the Zip file for my app if you, or anyone else, cares to take a look.

Regards

Edit:
The NoSuchFieldException refers to “imgviewwifi”, which is an ImageView that I am using as a button. This is not a FileNotFoundException. I think the error relates to my use of the line “rv.SetImage("ImgViewWIFI",LoadBitmap(File.DirAssets,"WIFIoff.png"))”
In “Sub ConfigureButtons”, but I don’t understand why it’s causing this error.
 

Attachments

  • Widget.zip
    85.3 KB · Views: 190
Last edited:
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
The views in the widget layout file must be children of the main panel.

Thank you Erel. I had four ImageViews, all of which should have been changed to have Panel1 as the Parent. I only changed the first and ignored the other three. I don’t know how I managed to forget the obvious. I guess I was so focused on learning more about widgets, that a standard of care for what I already should know took a back seat. I can now continue with my project, and hopefully retain what I learn from it.

Regards
 
Upvote 0
Top