0

oncreate:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    changingTextView = (TextView) findViewById(R.id.changingTextView);
    button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(this);
    button2 = (Button) findViewById(R.id.button2);
    button2.setOnClickListener(this);
    button3 = (Button) findViewById(R.id.button3);
    button3.setOnClickListener(this);
}

the script:

    public void onClick(View v) {


        if(v.getId() == R.id.button1){
        PaintDrawable drawable = (PaintDrawable) button1.getBackground();
        int color = drawable.getPaint().getColor();
        if(color==Color.WHITE)
            changingTextView.setText("continue");
        else
            changingTextView.setText("gameOVER");   
        }
        if(v.getId() == R.id.button2){
            PaintDrawable drawable = (PaintDrawable) button2.getBackground();
            int color = drawable.getPaint().getColor();
            if(color==Color.WHITE)
            changingTextView.setText("continue");
        else
            changingTextView.setText("gameOVER");
    } 
        if(v.getId() == R.id.button3){
            PaintDrawable drawable = (PaintDrawable) button3.getBackground();
            int color = drawable.getPaint().getColor();
            if(color==Color.WHITE)
            changingTextView.setText("continue");
        else
            changingTextView.setText("gameOVER");
    } 

here is the complete code+logcat image


main.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="com.example.bluetap.FirstActivity$PlaceholderFragment" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:minWidth="200dp"
    android:text="Press The White Button"
    android:textColor="@color/blue" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_below="@+id/button1"
    android:background="@android:color/white"
    android:minWidth="200dp"
    android:text="Press The White Button"
    android:textColor="@color/blue" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button2"
    android:layout_below="@+id/button2"
    android:minWidth="200dp"
    android:text="Press The White Button"
    android:textColor="@color/blue" />

<TextView
    android:id="@+id/changingTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button3"
    android:layout_centerVertical="true"
    android:text="text"
    android:textSize="20sp" />


in Eclipse there is no error in this code but when im running the application you can see everything and everything is fine but when you press the button1/2/3, the app crashes. can you help me find out why? my app just need to recognize if the button background is white, if does, it will continue, if not, it will pause and say game over.

0 Answers0