Android Question android.widget.TextView cannot be cast to android.widget.Button

Se6astian

New Member
Hi everybody, this is my first appearance on this forum. I hope somebody can help me with this problem.

I have a simple layout with 9 buttons and 6 labels.

This subroutine executes without any error:
Sub cmbKnop2_Click
For Each lbl As Label In Activity
lbl.Color=Colors.Blue
Next
End Sub


But this very similar routine does not:
Sub cmbKnop1_Click
For Each btn As Button In Activity
btn.Color=Colors.Red
Next
End Sub


The code compiles ok. But after executing (and coloring all buttons red) it pauses at the highlighted line.
If i continue execution (using F5 in the Editor) an error is displayed on my Android device:

"An error has occurred in sub main_cmbknop1_click ()
btn.Color=Colors.Red
java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button"

Has anybody an idea what causes this error?

Regards,
Se6astian
 

DonManfred

Expert
Licensed User
Longtime User
For Each btn As Button In Activity
will not work i suppose...

Try this
B4X:
    For Each v As View In Activity.GetAllViewsRecursive
        If v Is Label Then
            Dim lbl As Label = v
            ' change here the label
        End If
        If v Is Button Then
            Dim btn As Button = v
            ' change the button here
        End If
    Next
 
Upvote 0
Top