我是android编程的新手。我一直在使用eclipse并尝试开发两个玩家的tic tac toe游戏,但是只要有玩家获胜,该应用就会不断崩溃。我尝试打开一个新活动-一个对话框,提示玩家x获胜,两个按钮提示再次播放或退出。 PlayerOne和PlayerTwo类仅将内容视图设置为对话框。我试着试着赶上startActivity方法,但随后却什么也没发生,我连续得到三个X或O,游戏一直在运行,没有任何对话框显示。有人可以帮我吗?这是我的代码:
package com.rohan.tictactoe;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity implements OnClickListener{
Button t1,t2,t3,t4,t5,t6,t7,t8,t9;
int turn = 1;
int status = 0;
String s1,s2,s3,s4,s5,s6,s7,s8,s9 = "";
Intent i = new Intent("com.rohan.tictactoe.PlayerOne");
Intent x = new Intent("com.rohan.tictactoe.PlayerTwo");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initiallize();
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.t1:
if (turn ==1){
t1.setText("X");
turn =2;
s1 = "X";
}
else if(turn == 2){
t1.setText("O");
turn =1;
s1= "O";
}
winner();
break;
case R.id.t2:
if (turn ==1){
t2.setText("X");
turn =2;
s2 = "X";
}
else if(turn == 2){
t2.setText("O");
turn =1;
s2 = "O";
}
winner();
break;
case R.id.t3:
if (turn ==1){
t3.setText("X");
turn =2;
s3 = "X";
}
else if(turn == 2){
t3.setText("O");
turn =1;
s3 = "O";
}
winner();
break;
case R.id.t4:
if (turn ==1){
t4.setText("X");
turn =2;
s4 = "X";
t4.setEnabled(false);
}
else if(turn == 2){
t4.setText("O");
turn =1;
s4 = "O";
t4.setEnabled(false);
}
winner();
break;
case R.id.t5:
if (turn ==1){
t5.setText("X");
turn =2;
s5 = "X";
t5.setEnabled(false);
}
else if(turn == 2){
t5.setText("O");
turn =1;
s5 = "O";
t5.setEnabled(false);
}
winner();
break;
case R.id.t6:
if (turn ==1){
t6.setText("X");
turn =2;
s6 = "X";
t6.setEnabled(false);
}
else if(turn == 2){
t6.setText("O");
turn =1;
s6 = "O";
t6.setEnabled(false);
}
winner();
break;
case R.id.t7:
if (turn ==1){
t7.setText("X");
turn =2;
s7 = "X";
t7.setEnabled(false);
}
else if(turn == 2){
t7.setText("O");
turn =1;
s7 = "O";
t7.setEnabled(false);
}
winner();
break;
case R.id.t8:
if (turn ==1){
t8.setText("X");
turn =2;
s8 = "X";
t8.setEnabled(false);
}
else if(turn == 2){
t8.setText("O");
turn =1;
s8 = "O";
t8.setEnabled(false);
}
winner();
break;
case R.id.t9:
if (turn ==1){
t9.setText("X");
turn =2;
s9 = "X";
t9.setEnabled(false);
}
else if(turn == 2){
t9.setText("O");
turn =1;
s9 = "O";
t9.setEnabled(false);
}
winner();
break;
}
}
public void winner(){
if (s1 == s2 && s2 == s3 && s3 =="X"){
startActivity(i);
}
else if(s1 == s2 && s2 == s3 && s3 =="O"){
startActivity(x);
}
else if(s4 == s5 && s5 == s6 && s6 =="X"){
startActivity(i);
}
else if(s4 == s5 && s5 == s6 && s6 =="O"){
startActivity(x);
}
else if(s7 == s8 && s8 == s9 && s9 =="X"){
startActivity(i);
}
else if(s7 == s8 && s8 == s9 && s9 =="O"){
startActivity(x);
}
else if(s1 == s4 && s4 == s7 && s7 =="X"){
startActivity(i);
}
else if(s1 == s4 && s4 == s7 && s7 =="O"){
startActivity(x);
}
else if(s2 == s5 && s5 == s8 && s8 =="X"){
startActivity(i);
}
else if(s2 == s5 && s5 == s8 && s8 =="O"){
startActivity(x);
}
else if(s3 == s6 && s6 == s9 && s9 =="X"){
startActivity(i);
}
else if(s3 == s6 && s6 == s9 && s9 =="O"){
startActivity(x);
}
else if(s1 == s5 && s5 == s9 && s9 =="X"){
startActivity(i);
}
else if(s1 == s5 && s5 == s9 && s9 =="O"){
startActivity(x);
}
else if(s3 == s5 && s5 == s7 && s7 =="X"){
startActivity(i);
}
else if(s3 == s5 && s5 == s7 && s7 =="O"){
startActivity(x);
}else{
}
}
public void initiallize(){
t1 = (Button) findViewById(R.id.t1);
t2 = (Button) findViewById(R.id.t2);
t3 = (Button) findViewById(R.id.t3);
t4 = (Button) findViewById(R.id.t4);
t5 = (Button) findViewById(R.id.t5);
t6 = (Button) findViewById(R.id.t6);
t7 = (Button) findViewById(R.id.t7);
t8 = (Button) findViewById(R.id.t8);
t9 = (Button) findViewById(R.id.t9);
t1.setOnClickListener(this);
t2.setOnClickListener(this);
t3.setOnClickListener(this);
t4.setOnClickListener(this);
t5.setOnClickListener(this);
t6.setOnClickListener(this);
t7.setOnClickListener(this);
t8.setOnClickListener(this);
t9.setOnClickListener(this);
}
}
最佳答案
两件事,当您初始化意图时,您使用的是Intent(String action)作为构造函数。 com.rohan.tictactoe.PlayerOne
是动作吗?是你的班吗?如果是一堂课,那您真的不应该那样做。
尝试这个:
public void winner(){
Intent i = new Intent(this, PlayerOne.class);
Intent x = new Intent(this, PlayerTwo.class);
//blahblah rest of your code
}
其次,您需要将活动添加到您的AndroidManifest.xml中以使其正常运行。在您的AndroidManifest.xml中,必须在应用程序标记之间添加以下代码:
<activity android:name=".PlayerOne"/>
<activity android:name=".PlayerTwo"/>