简体   繁体   中英

Android App crashing when opening a new intent

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:

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);
 }


}

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.

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:

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

Add this Code in your Manifest between the existing

" <application> </application>":

Have you tried creating intent with this constructor.

Intent(Context packageContext, Class<?> cls)

Eg:

Intent i = new Intent(this, com.rohan.tictactoe.PlayerOne.class);

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.

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.

You can try this, goto 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM