【发布时间】:2015-02-19 23:49:10
【问题描述】:
第一次使用 Android 开发者,但过去使用过 C# 和 Java。
尝试制作一个简单的、类似于 Windows 8 的 GUI。目前,我有一个在 activity_main.xml 中设置了背景颜色的图块(ImageButton)。
<ImageButton
android:id="@+id/btn1"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="#FF0000"
android:onClick="changeColor"/>
我有一个函数可以改变 MainActivity.java 中的颜色。
public void changeColor(){
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
btn1.setBackgroundColor(Color.GREEN);
}
编译正常,但每次我点击红色方块时,应用程序都会崩溃。
我假设我缺少关于 Android 开发方式的一些基本内容,这导致了一个非常明显的错误。有没有比 ImageButtons 更好的方法?
谢谢!
【问题讨论】: