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:

What is my mistake here?
 

DavideV

Active Member
Licensed User
Longtime User

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
 
Upvote 0

Stefano Di Chiano

Active Member
Licensed User
The button sub is just this:
B4X:
Sub Button6_Click
    caricaPref(Button6, 6)
End Sub
It is the same for all the buttons.

What is pref and where and how is it defined.
pref is an array of int, I defined it as:
B4X:
Dim pref = Array(0, 0, 0, 0, 0, 0, 0, 0) As Int
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…