【问题标题】:Cannot resolve method 'setImageResource'(android.graphics.drawable.Drawable)无法解析方法“setImageResource”(android.graphics.drawable.Drawable)
【发布时间】:2015-09-11 01:40:15
【问题描述】:

我正在尝试更改 ImageButton 的来源,但由于某种原因,我不断收到此错误消息:无法解析方法 'setImageResource' (android.graphics.drawable.Drawable)。

这是我尝试使用的代码:

<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"/>

在mainActivity中:

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

知道如何解决这个问题吗?

【问题讨论】:

    标签: java android html image imagebutton


    【解决方案1】:

    我可以看到 2 个错误

    改变

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

    // 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.
    

    供您参考:http://developer.android.com/reference/android/widget/ImageView.html#setImageResource%28int%29

    【讨论】:

    • 是的,没错。您忘记将对象转换为使用 ImageButton 类
    • 谢谢,帮了大忙。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 2015-06-10
    • 2019-08-28
    • 2019-10-10
    • 2019-06-13
    • 2017-11-13
    相关资源
    最近更新 更多