Android Question App Widget Issue (Resolved)

shubas

Member
Licensed User
Hello Experts - I worked hard to get the Widget show, however, I face a new challenge now. I'm duplicating the code I saw in an example and its not working when I try to create the widget manually. Any ideas? Below is the code.

B4X:
#Region Module Attributes
    #StartAtBoot: False
#End Region
 
'Service module
Sub Process_Globals
    Dim rv As RemoteViews
    Dim var11 As String
    Dim var22 As String
    Dim setpref As PreferenceManager
    Dim wdSMS As PhoneSms
    Dim imageFiles() As String
    imageFiles = Array As String("send.png") 'file is added to the library and exists.
    Dim currentImage As Int
End Sub
Sub Service_Create
    'Set the widget to update every day
    rv = ConfigureHomeWidget("WSMS", "rv", 60, "SafetyFirst SMS",True)
    var11 = setpref.GetString("def1") '#
    var22 = setpref.GetString("def3") 'msg
'LOG shows that the values are fetched
End Sub
 
Sub Service_Start (StartingIntent As Intent)
    rv.HandleWidgetEvents(StartingIntent)
End Sub
 
Sub rv_RequestUpdate
    SetTime
    rv.UpdateWidget
End Sub
 
Sub rv_Disabled
    StopService("")
End Sub
 
Sub Service_Destroy
 
End Sub
 
Sub SetTime
    rv.SetText("Label1", "Send SMS [" & var22 & "] to " & var11)
End Sub
 
Sub imgB_Click
currentImage = (currentImage + 1) Mod imageFiles.Length
    rv.SetImage("imgB", LoadBitmap(File.DirAssets, imageFiles(currentImage)))
    rv.UpdateWidget
    rv.SetText("Label1","SMS Sent")
    Log("started")
    wdSMS.Send2(var11,var22,True,True)
    ToastMessageShow("Sent...",False)
    End Sub
Below is the error that I get when Adding the widget to the homescreen.

B4X:
** Service (widgetservice) Create **
 
 
java.lang.RuntimeException: java.lang.NoSuchFieldException: widgetservice_imgb
 
 
    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 com.shuvrobasu.IReachedSafelyApp.widgetservice._service_create(widgetservice.java:162)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
    at com.shuvrobasu.IReachedSafelyApp.widgetservice.onCreate(widgetservice.java:46)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:1945)
    at android.app.ActivityThread.access$2500(ActivityThread.java:117)
 
 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3683)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchFieldException: widgetservice_imgb
    at java.lang.ClassCache.findFieldByName(ClassCache.java:446)
    at java.lang.Class.getField(Class.java:864)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.getIdForView(RemoteViewsWrapper.java:93)
    ... 20 more
java.lang.RuntimeException: Unable to create service com.shuvrobasu.IReachedSafelyApp.widgetservice: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NoSuchFieldException: widgetservice_imgb
 
 
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:1955)
    at android.app.ActivityThread.access$2500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3683)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NoSuchFieldException: widgetservice_imgb
 
 
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
    at com.shuvrobasu.IReachedSafelyApp.widgetservice.onCreate(widgetservice.java:46)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:1945)
    ... 10 more
Caused by: java.lang.RuntimeException: java.lang.NoSuchFieldException: widgetservice_imgb
    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 com.shuvrobasu.IReachedSafelyApp.widgetservice._service_create(widgetservice.java:162)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    ... 13 more
Caused by: java.lang.NoSuchFieldException: widgetservice_imgb
    at java.lang.ClassCache.findFieldByName(ClassCache.java:446)
    at java.lang.Class.getField(Class.java:864)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.getIdForView(RemoteViewsWrapper.java:93)
    ... 20 more
** Service (widgetservice) Create **
 
 
java.lang.RuntimeException: java.lang.NoSuchFieldException: widgetservice_imgb
 
 
    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 com.shuvrobasu.IReachedSafelyApp.widgetservice._service_create(widgetservice.java:162)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
    at com.shuvrobasu.IReachedSafelyApp.widgetservice.onCreate(widgetservice.java:46)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:1945)
    at android.app.ActivityThread.access$2500(ActivityThread.java:117)
 
 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3683)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchFieldException: widgetservice_imgb
    at java.lang.ClassCache.findFieldByName(ClassCache.java:446)
    at java.lang.Class.getField(Class.java:864)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.getIdForView(RemoteViewsWrapper.java:93)
    ... 20 more
java.lang.RuntimeException: Unable to create service com.shuvrobasu.IReachedSafelyApp.widgetservice: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NoSuchFieldException: widgetservice_imgb
 
 
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:1955)
    at android.app.ActivityThread.access$2500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3683)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NoSuchFieldException: widgetservice_imgb
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
    at com.shuvrobasu.IReachedSafelyApp.widgetservice.onCreate(widgetservice.java:46)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:1945)
    ... 10 more
Caused by: java.lang.RuntimeException: java.lang.NoSuchFieldException: widgetservice_imgb
    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 com.shuvrobasu.IReachedSafelyApp.widgetservice._service_create(widgetservice.java:162)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    ... 13 more
Caused by: java.lang.NoSuchFieldException: widgetservice_imgb
    at java.lang.ClassCache.findFieldByName(ClassCache.java:446)
    at java.lang.Class.getField(Class.java:864)
    at anywheresoftware.b4a.objects.RemoteViewsWrapper.getIdForView(RemoteViewsWrapper.java:93)
    ... 20 more
Not sure what is going on here. I removed and added the files. Also "cleaned" the project. But no go. Any help will be highly appreciated.
============================================================
See posts #3 and #9 for more details.
 
Last edited:

shubas

Member
Licensed User
I dont know whether it is important or not, the layout file that gets created doesnt have the tags for the image view at all. It only has tags for the panel and the labels.
Is this something to do with the Designer?
 

shubas

Member
Licensed User
[Update] - I got to display the widget correctly by manually updating the widgetlayout file which wasnt adding the imageview for some reason. Though I had already added the picture to be filled in the image in the dir.assets, it was not getting saved in the res\drawable folder. I was getting errors continually while compiling and figured out that the image (png) file had to copied in this folder to make it work. So the good news is that the widget now shows in the homescreen without throwing any errors, but when I click the image, it doesnt react. I've a log statment as the first statement in the imageview_click sub. It doesnt trigger at all.

Any directions now?

I'll be logging off for the day. I'll respond back to any replies on Monday. Thanks in advance for your help.
 

shubas

Member
Licensed User
Attached is the ZIP file of the project Erel.

[Edit] - Removing as the issue was resoloved and the files weren't changed.
 
Last edited:

shubas

Member
Licensed User
I'm using that since it has been causing problems. When I made it RO it was compiling and working perfectly. From an APP standpoint. The issue is with the widgets. The APP is doing exactly what I expect it to do.
 

shubas

Member
Licensed User
Also I noticed that when I compile, files from the RES folder vanish. This is the error I get below. I've made the file RO to prevent deletion.
B4X:
B4A version 4.30
Parsing code.                           0.07
Compiling code.                         Error
Error compiling program.
Error description: Access to the path 'C:\OR\B4A\IReachedSafely\Objects\res\drawable\send.png' is denied.
Occurred on line: 18
rv = ConfigureHomeWidget("WSMS", "rv", 60, "SafetyFirst SMS",True)
Word: )
 

shubas

Member
Licensed User
After lot of trial and error, finally everything is working. The app and the widgets are all in snyc and working as expected. Lessons learned
  • The layout files are arbitrarily changed by B4A (Not sure if this is a bug or happening only in my system.). Best way to tackle this is make the layout files RO and compile. If everything is fine, then then make them RW and recompile again.
  • The issues with widget events - I tried buttons, labels and imageview. All work (click event for all).
  • To change anything at runtime (like image) use rv.setimage (rv = your remote view declaration variable name)
  • Please keep the files that you need in the "Files" folder to a minimum and size your pics based on need to reduce the overall size of the APK file. (I saw that the size of the APK suddenly grew two fold even though I hadn't added any new assets. Then i check the folder to see that I had exported the project and it got saved here and was included as part of the APK. I delete the file and remcopiled, immediately the fize size came down
  • If you are getting errors, read the error log, it usually points out where the problem lies. However I did see a sitaution where the error occured at run-time stating that an image file was missing but it was there and in RW mode. I had to make it RO compile couple of times to make it work. These are issues with res\drawable folder and ress\layout folder. The files get deleted even if you dont make any changes via the Designer.
Overall satified. Removing the attachments from above there wasn't any solutions that required it to be updated.
 
Last edited:
Top