0

我有一个小问题。我正在开发一个小井字游戏。

我有一个开始屏幕,您可以在其中输入玩家名称,并通过 putExtra 将该字符串提供给游戏活动。到目前为止,它的工作原理是,玩家名称显示在游戏活动中。

我现在的问题是:如果一个玩家赢得游戏,“NewGame”按钮就会变得可见。单击时,它应该使用 StartScreen 中给出的相同名称重新启动 Game-Activity。

这是它崩溃的地方。

这是我的代码和logcat:

开始屏幕:

package de.android.pali;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;

public class TicTacToeMenu extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tic_tac_toe_menu);

    final ImageButton createGame = (ImageButton)findViewById(R.id.newGameButton);
    final EditText spieler1 = (EditText)findViewById(R.id.addsp1);
    final EditText spieler2 = (EditText)findViewById(R.id.addsp2);
    final Intent newGame = new Intent(TicTacToeMenu.this, NewGame.class);

    createGame.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            newGame.putExtra("Name1", spieler1.getText().toString());
            newGame.putExtra("Name2", spieler2.getText().toString());

            startActivity(newGame);


        }
    });


}


}

游戏活动:

package de.android.pali;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class NewGame extends TicTacToeMenu{

int turn = 1;
int[] felder = new int[9];

boolean gotWinner = false;

ImageButton feld1;
ImageButton feld2;
ImageButton feld3;
ImageButton feld4;
ImageButton feld5;
ImageButton feld6;
ImageButton feld7;
ImageButton feld8;
ImageButton feld9;
Button anotherGame;
TextView spieler1;
TextView spieler2;
TextView ergebnis;




public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

    setupUI();


    setOnClickListeners(feld1, feld2, feld3, feld4, feld5, feld6, feld7,
            feld8, feld9);


}

public void setupUI() {

    feld1 = (ImageButton)findViewById(R.id.feld1);
    feld2 = (ImageButton)findViewById(R.id.feld2);
    feld3 = (ImageButton)findViewById(R.id.feld3);
    feld4 = (ImageButton)findViewById(R.id.feld4);
    feld5 = (ImageButton)findViewById(R.id.feld5);
    feld6 = (ImageButton)findViewById(R.id.feld6);
    feld7 = (ImageButton)findViewById(R.id.feld7);
    feld8 = (ImageButton)findViewById(R.id.feld8);
    feld9 = (ImageButton)findViewById(R.id.feld9);

    final TextView spieler1 = (TextView)findViewById(R.id.sp1name);
    final TextView spieler2 = (TextView)findViewById(R.id.sp2name);


    spieler1.setText(getIntent().getStringExtra("Name1").toString());
    spieler2.setText(getIntent().getStringExtra("Name2").toString());

    for(int x=0; x<9; x++){felder[x]=2;};
}

public void setOnClickListeners(final ImageButton feld1,
        final ImageButton feld2, final ImageButton feld3,
        final ImageButton feld4, final ImageButton feld5,
        final ImageButton feld6, final ImageButton feld7,
        final ImageButton feld8, final ImageButton feld9) {
    feld1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            feld1.setEnabled(false);
            feld1.setAlpha(1);
            if(turn==1){
            feld1.setBackgroundResource(R.drawable.o);
            turn = 0;
            felder[0] = 1;
            checkWinner();
            }
            else{
                feld1.setBackgroundResource(R.drawable.x);
                turn = 1;
                felder[0] = 0;
                checkWinner();
            }
        }


    });

    feld2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            feld2.setEnabled(false);
            if(turn==1){
                feld2.setBackgroundResource(R.drawable.o);
                turn = 0;
                felder[1] = 1;
                checkWinner();
                }
                else{
                    feld2.setBackgroundResource(R.drawable.x);
                    felder[1] = 0;
                    turn = 1;
                    checkWinner();

                }
        }
    });

    feld3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            feld3.setEnabled(false);
            if(turn==1){
                feld3.setBackgroundResource(R.drawable.o);
                turn = 0;
                felder[2] = 1;
                checkWinner();
                }
                else{
                    feld3.setBackgroundResource(R.drawable.x);
                    turn = 1;
                    felder[2] = 0;
                    checkWinner();
                };

        }
    });

    feld4.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            feld4.setEnabled(false);
            if(turn==1){
                feld4.setBackgroundResource(R.drawable.o);
                turn = 0;
                felder[3] = 1;
                checkWinner();
                }
                else{
                    feld4.setBackgroundResource(R.drawable.x);
                    felder[3] = 0;
                    turn = 1;
                    checkWinner();
                }

        }
    });

    feld5.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            feld5.setEnabled(false);

            if(turn==1){
                feld5.setBackgroundResource(R.drawable.o);
                turn = 0;
                felder[4] = 1;
                checkWinner();

                }
                else{
                    feld5.setBackgroundResource(R.drawable.x);
                    turn = 1;
                    felder[4] = 0;
                    checkWinner();
                }

        }
    });

    feld6.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            feld6.setEnabled(false);
            if(turn==1){
                feld6.setBackgroundResource(R.drawable.o);
                turn = 0;
                felder[5] = 1;
                checkWinner();
                }
                else{
                    feld6.setBackgroundResource(R.drawable.x);
                    turn = 1;
                    felder[5] = 0;
                    checkWinner();
                }

        }
    });

    feld7.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            feld7.setEnabled(false);
            if(turn==1){
                feld7.setBackgroundResource(R.drawable.o);
                turn = 0;
                felder[6] = 1;
                checkWinner();
                }
                else{
                    feld7.setBackgroundResource(R.drawable.x);
                    turn = 1;
                    felder[6] = 0;
                    checkWinner();
                }

        }
    });

    feld8.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            feld8.setEnabled(false);
            if(turn==1){
                feld8.setBackgroundResource(R.drawable.o);
                turn = 0;
                felder[7] = 1;
                checkWinner();
                }
                else{
                    feld8.setBackgroundResource(R.drawable.x);
                    turn = 1;
                    felder[7] = 0;
                    checkWinner();
                }

        }
    });

    feld9.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            feld9.setEnabled(false);
            if(turn==1){
                feld9.setBackgroundResource(R.drawable.o);
                turn = 0;
                felder[8] = 1;
                checkWinner();
                }
                else{
                    feld9.setBackgroundResource(R.drawable.x);
                    turn = 1;
                    felder[8] = 0;
                    checkWinner();
                }

        }
    });
}

public int getTurn() {
    int whois = turn;
    return whois;
}

public void checkWinner(){

    ergebnis = (TextView)findViewById(R.id.winners);

    if( (felder[0]==1 & felder[1]==1 & felder[2]==1) || 
        (felder[3]==1 & felder[4]==1 & felder[5]==1) || 
        (felder[6]==1 & felder[7]==1 & felder[8]==1) || 
        (felder[0]==1 & felder[4]==1 & felder[8]==1) || 
        (felder[6]==1 & felder[4]==1 & felder[2]==1) || 
        (felder[0]==1 & felder[3]==1 & felder[6]==1) || 
        (felder[1]==1 & felder[4]==1 & felder[7]==1) || 
        (felder[2]==1 & felder[5]==1 & felder[8]==1)    ){

        ergebnis.setText((getIntent().getStringExtra("Name1").toString()) + " " + getString((R.string.gewinnt)));
        gotWinner = true;

        }else
            { }
    if( (felder[0]==1 & felder[1]==1 & felder[2]==1) || 
            (felder[3]==0 & felder[4]==0 & felder[5]==0) || 
            (felder[6]==0 & felder[7]==0 & felder[8]==0) || 
            (felder[0]==0 & felder[4]==0 & felder[8]==0) || 
            (felder[6]==0 & felder[4]==0 & felder[2]==0) || 
            (felder[0]==0 & felder[3]==0 & felder[6]==0) || 
            (felder[1]==0 & felder[4]==0 & felder[7]==0) || 
            (felder[2]==0 & felder[5]==0 & felder[8]==0)    ){

            ergebnis.setText((getIntent().getStringExtra("Name2").toString()) + " " + (getString(R.string.gewinnt)));
            gotWinner = true;

            }else
                { }

    checkIfGotWinner();

}

public void checkIfGotWinner() {

    anotherGame = (Button)findViewById(R.id.anotherGame);


    if(gotWinner == true){


        anotherGame.setVisibility(1);

        final Intent anotGame = new Intent(NewGame.this,NewGame.class);

            anotGame.putExtra("Name1", spieler1.getText().toString());
        anotGame.putExtra("Name2", spieler2.getText().toString());

        feld1.setEnabled(false);
        feld2.setEnabled(false);
        feld3.setEnabled(false);
        feld4.setEnabled(false);
        feld5.setEnabled(false);
        feld6.setEnabled(false);
        feld7.setEnabled(false);
        feld8.setEnabled(false);
        feld9.setEnabled(false);

        final TextView spieler1 = (TextView)findViewById(R.id.sp1name);
        final TextView spieler2 = (TextView)findViewById(R.id.sp2name);
        final ImageView sp1Icon = (ImageView)findViewById(R.id.sp1icon);
        final ImageView sp2Icon = (ImageView)findViewById(R.id.sp2icon);
        final View trenn = (View)findViewById(R.id.trennlinie);
        ergebnis = (TextView)findViewById(R.id.winners);

        spieler1.setAlpha(0);
        spieler2.setAlpha(0);
        sp1Icon.setAlpha(0);
        sp2Icon.setAlpha(0);
        trenn.setAlpha(0);
        ergebnis.setVisibility(1);


        anotherGame.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(anotGame);

            }
        });

    };

    if(feld1.isEnabled()==false & feld2.isEnabled()==false & feld3.isEnabled()==false & feld4.isEnabled()==false & feld5.isEnabled()==false & feld6.isEnabled()==false &
            feld7.isEnabled()==false & feld8.isEnabled()==false & feld9.isEnabled()==false & gotWinner == false){ergebnis.setText(R.string.draw);


            anotherGame.setVisibility(1);

            final Intent anotGame = new Intent(NewGame.this,NewGame.class);



            anotherGame.setOnClickListener(new OnClickListener() {


                @Override
                public void onClick(View v) {



                    startActivity(anotGame);

                }
            });
    }

    else{}
}
}

日志猫:

11-29 15:59:30.681: E/AndroidRuntime(2683): FATAL EXCEPTION: main
11-29 15:59:30.681: E/AndroidRuntime(2683): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.android.pali/de.android.pali.NewGame}: java.lang.NullPointerException
11-29 15:59:30.681: E/AndroidRuntime(2683):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
11-29 15:59:30.681: E/AndroidRuntime(2683):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
11-29 15:59:30.681: E/AndroidRuntime(2683):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
11-29 15:59:30.681: E/AndroidRuntime(2683):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
11-29 15:59:30.681: E/AndroidRuntime(2683):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 15:59:30.681: E/AndroidRuntime(2683):     at android.os.Looper.loop(Looper.java:137)
11-29 15:59:30.681: E/AndroidRuntime(2683):     at android.app.ActivityThread.main(ActivityThread.java:4424)
11-29 15:59:30.681: E/AndroidRuntime(2683):     at java.lang.reflect.Method.invokeNative(Native Method)
11-29 15:59:30.681: E/AndroidRuntime(2683):     at java.lang.reflect.Method.invoke(Method.java:511)
11-29 15:59:30.681: E/AndroidRuntime(2683):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-29 15:59:30.681: E/AndroidRuntime(2683):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-29 15:59:30.681: E/AndroidRuntime(2683):     at dalvik.system.NativeStart.main(Native Method)
11-29 15:59:30.681: E/AndroidRuntime(2683): Caused by: java.lang.NullPointerException
11-29 15:59:30.681: E/AndroidRuntime(2683):     at de.android.pali.NewGame.setupUI(NewGame.java:65)
11-29 15:59:30.681: E/AndroidRuntime(2683):     at de.android.pali.NewGame.onCreate(NewGame.java:40)
11-29 15:59:30.681: E/AndroidRuntime(2683):     at android.app.Activity.performCreate(Activity.java:4465)
11-29 15:59:30.681: E/AndroidRuntime(2683):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-29 15:59:30.681: E/AndroidRuntime(2683):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
11-29 15:59:30.681: E/AndroidRuntime(2683):     ... 11 more

希望任何人都可以提供帮助。

4

1 回答 1

0

首先,在它自己的内部开始相同的活动不是好的做法。就像蛇吞下自己的尾巴一样。所以你需要编写一些可以直接重置游戏的逻辑。看起来用户第一次开始

提示:通过将其设置为空来删除所有变量的值,仅保留用户名(制作一个将为您执行此操作的函数)

于 2012-11-29T14:54:30.097 回答