我想在android studio上做一个tic-tac-toe。所以我这样做:
public class MainActivity2Activity extends ActionBarActivity {
char a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
final Button button0=(Button) findViewById(R.id.button0);
final Button button1=(Button) findViewById(R.id.button1);
final Button button2=(Button) findViewById(R.id.button2);
final Button button3=(Button) findViewById(R.id.button3);
final Button button4=(Button) findViewById(R.id.button4);
final Button button5=(Button) findViewById(R.id.button5);
final Button button6=(Button) findViewById(R.id.button6);
final Button button7=(Button) findViewById(R.id.button7);
final Button button8=(Button) findViewById(R.id.button8);
final Button player1=(Button) findViewById(R.id.player2);
final Button player2=(Button) findViewById(R.id.player2);
player1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
a='X';
}
});
player2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
a='O';
}
});
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button0.setText(a);
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button1.setText(a);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button2.setText(a);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button3.setText(a);
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button4.setText(a);
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button5.setText(a);
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button6.setText(a);
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button7.setText(a);
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button8.setText(a);
}
});
但是当我打开应用程序然后点击底部写一个x或者o应用程序说:应用程序异常停止。我该如何解决,请回答我..
答案 0 :(得分:0)
setText方法接受String变量
将变量的类型更改为String并使用“而不是'
public final void setText(CharSequence text)
设置TextView的字符串值
答案 1 :(得分:0)
你为两个玩家使用了两次相同的id玩家