Android Question Passing a button as parameter

Stefano Di Chiano

Active Member
Licensed User
Hi,
I was trying to make a sub that i could call from any button, passing the same button as a parameter. The sub should only change the button background image and changing the related cell of an array.
This is the sub I made:
Sub:
Sub caricaPref(btnPref as Button, number as Int)
    If pref(number-1) = 0 Then
        pref(number-1) = 1
        myImage.Initialize(File.DirAssets, "immagine"&number&"b.png")
        btnPref.SetBackgroundImage(myImage)
    Else
        pref(number-1) = 0
        myImage.Initialize(File.DirAssets, "immagine"&number&".png")
        btnPref.SetBackgroundImage(myImage)
    End If
End Sub

This is the error I get when I click on one of the buttons:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (registrazione) Create, isFirst = true **
** Activity (registrazione) Resume **
Error occurred on line: 245 (Registrazione)
java.lang.IllegalArgumentException: field it.uppeee.uppeee.registrazione._pref has type int[], got java.lang.Object[]
at java.lang.reflect.Field.set(Native Method)
at anywheresoftware.b4a.shell.Shell.setStateBeforeUserSub(Shell.java:509)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:411)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:5646)
at android.view.View$PerformClick.run(View.java:22473)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6517)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
** Activity (registrazione) Pause, UserClosed = true **
** Service (starter) Destroy (ignored)**

What is my mistake here?
 

DavideV

Active Member
Licensed User
Longtime User
Hi,
I was trying to make a sub that i could call from any button, passing the same button as a parameter. The sub should only change the button background image and changing the related cell of an array.
This is the sub I made:
Sub:
Sub caricaPref(btnPref as Button, number as Int)
    If pref(number-1) = 0 Then
        pref(number-1) = 1
        myImage.Initialize(File.DirAssets, "immagine"&number&"b.png")
        btnPref.SetBackgroundImage(myImage)
    Else
        pref(number-1) = 0
        myImage.Initialize(File.DirAssets, "immagine"&number&".png")
        btnPref.SetBackgroundImage(myImage)
    End If
End Sub

This is the error I get when I click on one of the buttons:


What is my mistake here?

Hi, the key is here:
Error occurred on line: 245 (Registrazione)
java.lang.IllegalArgumentException: field it.uppeee.uppeee.registrazione._pref has type int[], got java.lang.Object[]

You are passing an object to a sub that requires an int

Not sure if it is related to the above sub you wrote.

Check better your code :rolleyes:
 
Upvote 0
Top