Android Question button.background problem [solved]

Hans- Joachim Krahe

Active Member
Licensed User
Longtime User
hi there, does anyone know the reason:

I have buttons on layout 1, I paint with button background
B4X:
Dim poCD_Start As ColorDrawable ' in globals
poCD_Start.Initialize(Colors. RGB(200,120,120), 110dip) ' in Activity_Create
Dim noteObject As Object = mapNoteZuButton.GetDefault( noteStr, "c0")
Dim notebutton As Button = noteObject
notebutton.background = poCD_Start' in sub

this works fine.

if I change to layout2 and return to layout1 then,

B4X:
button.background = poCD_Start

is not yet executed - there is no error and the noteObject and noteButton and poCD_Start seem to be filled correctly...

dont know, why...



thanks for interest.


... needed to fill a map again - don't know why, but works now:)
 
Last edited:

Hans- Joachim Krahe

Active Member
Licensed User
Longtime User
ups, Erel, I am not Albert Einstein... What is the drawable Instanz?

I am not even able to install ssl on my Iis10 Server. By the way; could anyone help me to install it by teamviever for fair donation at success? -)
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I am not even able to install ssl on my Iis10 Server. By the way; could anyone help me to install it by teamviever for fair donation at success?
This question has nothing to do with this thread.

What is the drawable Instanz?
This code creates a single drawable instance:
B4X:
Dim poCD_Start As ColorDrawable ' in globals
poCD_Start.Initialize(Colors. RGB(200,120,120), 110dip)
The object created should only be set to a single view. If you do something like this then you will get "strange" issues:
B4X:
Button1.Background = poCD_Start
Button2.Background = poCD_Start
 
Upvote 0

Hans- Joachim Krahe

Active Member
Licensed User
Longtime User
hm, my layout has about 120 Buttons, I use the same instance of colorDrawable for - without problems, and change them in case of events.

B4X:
                        If note < 48 Then
                            myButton(buttonCount).Background = poCD_min
                        Else If note < 60 Then
                            myButton(buttonCount).Background = poCD_0
                        Else If note < 72 Then
                            myButton(buttonCount).Background = poCD_1
                        Else If note < 84 Then
                            myButton(buttonCount).Background = poCD_2
                        Else If note < 96 Then
                            myButton(buttonCount).Background = poCD_3
                        Else
                       ' ------------------------------
                      
            Dim keyboardButton As Button = mapNoteZuButton.GetDefault(                                                                                                noteStr.Replace("*","") & "#", "c0")
          
            If intent = -112  Or intent = -111 Then  ' 0x90 0x91
                notebutton.Background = poCD_Start
                Try
                    keyboardButton.Background = poCD_Start
                Catch
                    Log(LastException)
                End Try
            Else

upload_2018-12-2_11-59-29.png



ist the reason mayby, I took instances from the buttons before? going back to the layout, I need to read the buttons again to the list - means, I built a new instance of buttons probably ...


B4X:
    For Each v As View In Panel1.GetAllViewsRecursive
        If (GetType(V) = "android.widget.Button") Then
            buttonCount = buttonCount + 1 '
            myButton(buttonCount) = v

... have no errors even not in longTime test...

got a button_click sub for every butten. Would be fine, to have a central sub for button_click, but don't find a way...
 
Last edited:
Upvote 0

Hans- Joachim Krahe

Active Member
Licensed User
Longtime User
ok, try it. thank you

B4X:
Sub CreateBackground (intent As Int,midiInt As Int) As ColorDrawable
    Dim cd As ColorDrawable
    Try
 

        cd.Initialize(Colors. RGB(200,120,120), 110dip)
        If intent = 0x90  Or intent = 0x91 Then  ' 0x90 0x91

            cd.Initialize(Colors.RGB(200,120,120), 110dip)
            Return cd                                                              ' ---------  error line 575
        Else
            If midiInt < 48 Then
                cd.Initialize(Colors.RGB(210,210,150),110dip)
            Else If midiInt < 60 Then
                cd.Initialize(Colors.RGB(220,220,170),110dip)
            Else If midiInt < 72 Then
                cd.Initialize(Colors.RGB(230,230,190),110dip)
            Else If midiInt < 84 Then
                cd.Initialize(Colors.RGB(240,240,220),110dip)
            Else If midiInt < 96 Then
                cd.Initialize(Colors.RGB(255,255,240),110dip)
            End If
            Return cd
'        Dim keyString As String = noteStr.Replace("*","") & "#"
'        keyboardButton.Background = mapButtonZuBackground.Get(keyString)
        End If
 
    Catch
        Log(LastException)
    End Try
'    ..
    Return cd
End Sub


Error occurred on line: 575 (Main)
java.lang.ClassCastException: java.lang.String cannot be cast to android.view.View
at anywheresoftware.b4a.objects.ViewWrapper.setBackground(ViewWrapper.java:105)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:733)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:352)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.objects.CompoundButtonWrapper$1.onCheckedChanged(CompoundButtonWrapper.java:44)
at android.widget.CompoundButton.setChecked(CompoundButton.java:154)
at android.widget.CompoundButton.toggle(CompoundButton.java:113)
at android.widget.CompoundButton.performClick(CompoundButton.java:118)
at android.view.View$PerformClick.run(View.java:20262)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)


hmmm, difficult stuff... cannot comprehend casting error...

ok, error is not consistent - does not go to catch! only rises, if sub is called out of a loop, not if it is called from incoming data. Maybe time sensitive..

error occurs, when 'intent' = 53 or 'intent' = 67. Very obscur...


...got it!
 
Last edited:
Upvote 0
Top