Android Question Runtime exception accessing a class: Field: ba not found in: anywheresoftware.b4a.BALayout

Gary Milne

Active Member
Licensed User
Longtime User
I'm trying to create a general function that will do something to all instances of a specific class within a parent. I think this should work.

B4X:
For Each Class As MyClass In PanelName
     Class.Color(Colors.Blue)
Next

But it blows up with this error.
Error occurred on line: 129 (Main)
java.lang.RuntimeException: Field: ba not found in: anywheresoftware.b4a.BALayout
at anywheresoftware.b4a.shell.Shell$FieldCache.getField(Shell.java:865)
at anywheresoftware.b4a.shell.Shell.getField(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:346)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:244)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:132)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
** Activity (main) Resume **

If I do something similar with a built in view it works, such as this.
B4X:
For Each myPanel As Panel In PanelName
     myPanel.Color = Colors.Blue
Next

I realize the call within the class example and the call within the panel example are different but the methods are correct in their respective usage.

I'm stumped, any help appreciated.

Thanks.
 
Last edited:

Gary Milne

Active Member
Licensed User
Longtime User
Can you create a small example and upload it?

Here is a simple file. You will need to copy the files from the directory SmartHost into your Libraries folder.
Just to reiterate what I'm trying to do. I would like to recurse through all of the objects within a parent, identify those that are of the type SmartHost and then connect to those SmartHosts to execute functions within them.

I'm hoping to do a limited release of this next week once I'm done with the demo app. I think you will find it's capabilities very interesting.
 
Upvote 0

Gary Milne

Active Member
Licensed User
Longtime User
Sorry Erel, there was a leftover reference. This is a re-compiled version. I've included those supplementary files just in case (SH_Colors, and SH_Icons) but you should not need them in this simple test.
You are right in saying that you do not see the same code as there was in my original post. I simplified it as you requested but it generates the exact same error as you will see.
I'm sending this via e-Mail as it was too big for me to post. As a side benefit it's nice not to have old versions floating around in public places.
I'll post a copy of this to to the message board also for the sake of continuity. Thanks again, I'm excited to find a fix for this as it's my last big issue.

P.S. I deleted the prior version from this board.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are two problems in your code.
1. The For Each block will go over all views. Not just the views with the specific type.
2. The view that you are interested in is actually a Panel. Because this is the view that gets added to the layout.

You can set the class instance as the panel's tag property (in Initialize sub):
B4X:
Host.Back.Tag = Me

Later you can do something like:
B4X:
For Each v As View in Activity.GetAllViewsRecursive
 If v.Tag <> Null And v.Tag Is SmartHost_ Then
  Dim sh As SmartHost_ = v.Tag
 End If
Next
 
Upvote 0

Gary Milne

Active Member
Licensed User
Longtime User
Thank you so much Erel. I knew I could store something other than a primitive in the Tag but this is a great use case for that capability. Already implemented and it works wonderfully, a very elegant solution also.
 
Upvote 0

Gary Milne

Active Member
Licensed User
Longtime User
One last question on this. Everything works great with one exception. If the host panel (the one that holds the Tag) has a GradientDrawable applied then it no longer works with the above test. If I remove the Gradient it once again works.

What is going on behind the scenes when a Gradient is applied and is there an alternate test under those conditions?
 
Upvote 0

Gary Milne

Active Member
Licensed User
Longtime User
It's strange, I spend about 45 minutes yesterday isolating the problem as it was quite predictable. But today it is working as I would expect and I can't explain it.

Thanks again for your help.
 
Upvote 0
Top