0

I am new to android development. I am trying to create a tic tac toe game, and currently I want my buttons to be labelled with an X on click but when I run the program and click a button nothin happens. My activity.java file is below:

 package com.android.tictactoe;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class TictactoeActivity extends Activity implements Button.OnClickListener{
 Button one, two, three, four, five, six, seven, eight, nine;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle icicle) {
 super.onCreate(icicle);
 setContentView(R.layout.main);
 one=(Button)findViewById(R.id.one);
 one.setOnClickListener(this);
 two=(Button)findViewById(R.id.two);
 two.setOnClickListener(this);
 three=(Button)findViewById(R.id.three);
 three.setOnClickListener(this);
 four=(Button)findViewById(R.id.four);
 four.setOnClickListener(this);
 five=(Button)findViewById(R.id.five);
 five.setOnClickListener(this);
 six=(Button)findViewById(R.id.six);
 six.setOnClickListener(this);
 seven=(Button)findViewById(R.id.seven);
 seven.setOnClickListener(this);
 eight=(Button)findViewById(R.id.eight);
 eight.setOnClickListener(this);
 nine=(Button)findViewById(R.id.nine);
 nine.setOnClickListener(this);
 }
public void OnClick(Button button, int CheckedId){
 if(CheckedId==R.id.one && button.getText().equals("")){
 button.setText("X");
 }
 else if(CheckedId==R.id.two && button.getText().equals("")){
 button.setText("X");
 }
 else if(CheckedId==R.id.three && button.getText().equals("")){
 button.setText("X");
 }
 else if(CheckedId==R.id.four && button.getText().equals("")){
 button.setText("X");
 }
 else if(CheckedId==R.id.five && button.getText().equals("")){
 button.setText("X");
 }
 else if(CheckedId==R.id.six && button.getText().equals("")){
 button.setText("X");
 }
 else if(CheckedId==R.id.seven && button.getText().equals("")){
 button.setText("X");
 }
 else if(CheckedId==R.id.eight && button.getText().equals("")){
 button.setText("X");
 }
 button.setText("X");
}
@Override
public void onClick(View v) {
 // TODO Auto-generated method stub
}
}

my main.xml is below:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:stretchColumns="0" >
 <TableRow>
 <TextView
 android:text="Fizzle TicTacToe"
 android:textStyle="bold" 
 android:textSize="30dip"
 android:textColor="#ffaa88"
 />
 </TableRow>
 <TableRow> 
<Button
 android:textColor="#440000"
 android:textSize="40dip"
 android:textStyle="bold"
 android:text=""
 android:id="@+id/one"
android:layout_span="1" 
android:layout_width="0dip"
android:layout_height="100dip"
android:layout_weight="30"/>
<Button
 android:textColor="#440000"
 android:textSize="40dip"
 android:textStyle="bold"
 android:text=""
 android:id="@+id/two"
 android:layout_span="1" 
android:layout_width="0dip" 
android:layout_height="100dip"
android:layout_weight="30"/>
<Button
 android:textColor="#440000"
 android:textSize="40dip"
 android:textStyle="bold"
 android:text=""
 android:id="@+id/three"
android:layout_span="1" 
android:layout_width="0dip" 
android:layout_height="100dip"
android:layout_weight="30"/>
</TableRow>
 <TableRow> 
<Button
 android:textColor="#440000"
 android:textSize="40dip"
 android:textStyle="bold"
 android:text=""
 android:id="@+id/four"
android:layout_span="1" 
android:layout_width="0dip"
android:layout_height="100dip"
android:layout_weight="30"/>
<Button
 android:textColor="#440000"
 android:textSize="40dip"
 android:textStyle="bold"
 android:text=""
 android:id="@+id/five"
 android:layout_span="1" 
android:layout_width="0dip" 
android:layout_height="100dip"
android:layout_weight="30"/>
<Button
 android:textColor="#440000"
 android:textSize="40dip"
 android:textStyle="bold"
 android:text=""
 android:id="@+id/six"
android:layout_span="1" 
android:layout_width="0dip" 
android:layout_height="100dip"
android:layout_weight="30"/>
</TableRow>
 <TableRow> 
<Button
 android:textColor="#440000"
 android:textSize="40dip"
 android:textStyle="bold"
 android:text=""
 android:id="@+id/seven"
android:layout_span="1" 
android:layout_width="0dip"
android:layout_height="100dip"
android:layout_weight="30"/>
<Button
 android:textColor="#440000"
 android:textSize="40dip"
 android:textStyle="bold"
 android:text=""
 android:id="@+id/eight"
 android:layout_span="1" 
android:layout_width="0dip" 
android:layout_height="100dip"
android:layout_weight="30"/>
<Button
 android:textColor="#440000"
 android:textSize="40dip"
 android:textStyle="bold"
 android:text=""
 android:id="@+id/nine"
android:layout_span="1" 
android:layout_width="0dip" 
android:layout_height="100dip"
android:layout_weight="30"/>
</TableRow>
</TableLayout>
asked Jun 28, 2015 at 12:25
0

2 Answers 2

2

You need to provide the onClick behaviour on the overrided method not your own method.

@Override
public void onClick(View v) {
 OnClick((Button)v, v.getId());
}
N J
27.5k14 gold badges80 silver badges97 bronze badges
answered Jun 28, 2015 at 12:28
Sign up to request clarification or add additional context in comments.

Comments

0

Your OnClick method is not valid, copy his content to onClick method with overide

answered Jun 28, 2015 at 12:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.