gpt4 book ai didi

java - 打开新 Intent 时 Android 应用程序崩溃

转载 作者:行者123 更新时间:2023-12-01 18:28:52 26 4
gpt4 key购买 nike

我是 Android 编程新手。我一直在使用 eclipse 并尝试开发一个两人井字棋游戏,但是每当玩家获胜时该应用程序就会崩溃。我尝试打开一个新 Activity - 一个对话框显示玩家 x 获胜,两个按钮显示再次播放或退出。 PlayerOne 和 PlayerTwo 类只是将内容 View 设置为对话框。我尝试在 startActivity 方法周围放置 try 和 catch,但没有任何反应,我连续得到三个 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 时,你使用 Intent(String action) 作为构造函数。 com.rohan.tictactoe.PlayerOne 是一个 Action 吗?是你的类(class)吗?如果这是一个类,你真的不应该这样做。

试试这个:

public void winner(){
Intent i = new Intent(this, PlayerOne.class);
Intent x = new Intent(this, PlayerTwo.class);

//blahblah rest of your code
}

其次,您需要将 Activity 添加到 AndroidManifest.xml 中才能使其正常工作。在 AndroidManifest.xml 中,您必须在应用程序标记之间添加以下内容:

<activity android:name=".PlayerOne"/>
<activity android:name=".PlayerTwo"/>

关于java - 打开新 Intent 时 Android 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25043917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com