eclipse 打开新 Intent 时 Android 应用程序崩溃
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25043917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android App crashing when opening a new intent
提问by rohan510
I'm new to to android programming. I've been using eclipse and trying to develop a two player tic tac toe game, but the app keeps crashing whenever a player wins. I try to open a new activity - a dialog saying player x wins and two buttons saying play again or exit. PlayerOne and PlayerTwo classes just sets the content view to the dialogs. I tried putting try and catch around the startActivity methods but then nothing happens I get three X's or O's in a row, the game just keeps running without any dialogs showing. So can anyone help me? Here is me code:
我是 android 编程的新手。我一直在使用 eclipse 并尝试开发一个两人玩的井字游戏,但是只要玩家获胜,该应用程序就会不断崩溃。我尝试打开一个新活动 - 一个对话框说玩家 x 获胜,两个按钮说再次播放或退出。PlayerOne 和 PlayerTwo 类只是将内容视图设置为对话框。我尝试将 try 和 catch 放在 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);
}
}
回答by Otra
Two things, when you initialize your intent, you're using Intent(String action) as the constructor. Is com.rohan.tictactoe.PlayerOne
an action? Is it your class? If it's a class, you really shouldn't be doing it that way.
两件事,当你初始化你的意图时,你使用 Intent(String action) 作为构造函数。是com.rohan.tictactoe.PlayerOne
行动吗?是你的课吗?如果是一堂课,你真的不应该那样做。
Try this:
尝试这个:
public void winner(){
Intent i = new Intent(this, PlayerOne.class);
Intent x = new Intent(this, PlayerTwo.class);
//blahblah rest of your code
}
Secondly, you need to add the activities to your AndroidManifest.xml in order for it to work. Inside your AndroidManifest.xml, you must add this between your application tags:
其次,您需要将活动添加到您的 AndroidManifest.xml 以使其工作。在您的 AndroidManifest.xml 中,您必须在应用程序标签之间添加以下内容:
<activity android:name=".PlayerOne"/>
<activity android:name=".PlayerTwo"/>
回答by unknown
To start a new activity you are using StartActivity(your_intent) which is good. But problem with your intent. For intent your should pass first argument as content of activity and second as activity. Like this for starting playerOne with intent i.
要开始一项新活动,您正在使用 StartActivity(your_intent) 这很好。但是你的意图有问题。出于意图,您应该将第一个参数作为活动内容传递,第二个作为活动传递。像这样以意图 i 启动 playerOne。
Intent intent=new Intent(MainActivity.this, playerOne.class);
startActivity(intent);
General Structure for Intent: Intent intentname=new Intent(InvokingActivity.this,InvokedActivity.class) I hope this Helps.
Intent 的一般结构: Intent intentname=new Intent(InvokingActivity.this,InvokedActivity.class) 我希望这有帮助。
回答by Mayuresh Satao
You can try this, gotoBuild->Clean Project->Rebuild Project->Run
你可以试试这个, 转到Build->Clean Project->Rebuild Project->Run
After cleaning the project the previous build will be removed and so as the error if present and then after rebuild new build will be created depending on your current code. So might solve the error
清理项目后,先前的构建将被删除,因此如果存在错误,则在重建后将根据您当前的代码创建新的构建。所以可能会解决错误
回答by user3884522
<activity
android:name=".Main"
</activity>
Add this Code in your Manifest between the existing
在现有的清单之间添加此代码
" <application> </application>":
" <application> </application>":
回答by bajicdusko
Have you tried creating intent with this constructor.
您是否尝试过使用此构造函数创建意图。
Intent(Context packageContext, Class<?> cls)
Eg:
例如:
Intent i = new Intent(this, com.rohan.tictactoe.PlayerOne.class);