Cannot resolve method 'setImageResource'(android.graphics.drawable.Drawable)

4.4k views Asked by At

I'm trying to change the source of my ImageButton, but for some reason I keep getting this error message: Cannot resolve method 'setImageResource' (android.graphics.drawable.Drawable).

Here's the code im trying to use:

<ImageButton
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:id="@+id/cuenta_1"
        android:text="cuenta1"
        android:background="@mipmap/ic_fondo"
        android:layout_toLeftOf="@id/agregarCuenta"/>

And in the mainActivity:

Drawable logo = new BitmapDrawable(bitmap);
                        findViewById(R.id.cuenta_2).setImageResource(logo);

Any idea on how I can fix this?

1

There are 1 answers

1
Phantômaxx On BEST ANSWER

I can see 2 errors

Change

findViewById(R.id.cuenta_2).setImageResource(logo);

to

// Create and cast an object of type ImageButton, with the proper id
ImageButton imb = (ImageButton) findViewById(R.id.cuenta_1); // you were trying to find cuenta_2, which is not in the layout
imb.setImageResource(R.drawables.some_image); // setImageResource takes an int, not a drawable.

For your reference: http://developer.android.com/reference/android/widget/ImageView.html#setImageResource%28int%29