当我单击按钮时,我正在尝试更改视图背景的颜色,但是应用程序无法使用我现在拥有的代码打开我不知道问题是什么..
代码:
package on.click.button;
import android.support.v7.app.ActionBarActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class MainActivity extends ActionBarActivity {
private ImageButton imagebutton;
private ImageButton imagebuttonRED;
private ImageButton imagebuttonBLUE;
private ImageButton imagebuttonYELW;
private ImageButton imagebuttonGRN;
private View layout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imagebuttonRED = (ImageButton) findViewById(R.id.imageButton1);
imagebuttonBLUE = (ImageButton) findViewById(R.id.imageButton3);
imagebuttonYELW = (ImageButton) findViewById(R.id.imageButton2);
imagebuttonGRN = (ImageButton) findViewById(R.id.imageButton4);
layout = (View) findViewById(R.layout.activity_main);
imagebutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.activity_main);
}
});
imagebuttonRED.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(layout);
layout.setBackgroundColor(Color.GREEN);
}
});
imagebuttonBLUE.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(layout);
layout.setBackgroundColor(Color.RED);
}
});
imagebuttonYELW.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(layout);
layout.setBackgroundColor(Color.YELLOW);
}
});
imagebuttonGRN.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(layout);
layout.setBackgroundColor(Color.BLUE);
}
});
}
}
main_activity.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="nl.aventus.delaatstestap.MainActivity" >
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageButton2"
android:layout_marginLeft="14dp"
android:layout_toRightOf="@+id/imageButton2"
android:src="@drawable/blauw" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:src="@drawable/rood" />
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageButton2"
android:layout_below="@+id/imageButton2"
android:src="@drawable/flash" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageButton1"
android:layout_centerHorizontal="true"
android:src="@drawable/groen" />
<View
android:id="@+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton4"
android:layout_centerHorizontal="true"
android:background="#FF0000" />
<ImageButton
android:id="@+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/view1"
android:layout_alignRight="@+id/imageButton1"
android:layout_marginRight="19dp"
android:src="@drawable/min" />
<ImageButton
android:id="@+id/imageButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/view1"
android:layout_alignLeft="@+id/imageButton3"
android:layout_marginLeft="18dp"
android:src="@drawable/plus" />
</RelativeLayout>
请帮忙!