Java错误,应用程序崩溃,(宗地大小= 10722432)

时间:2016-10-16 17:32:50

标签: java android class

我目前正在制作一个TicTacToe应用程序,因为我的一个功能变得非常庞大,所以我决定将它放在另一个类中。该应用程序之前工作但现在当我想在我的手机上启动应用程序时,它会立即崩溃。这是我得到的错误:

报告崩溃时出错                                                                     android.os。

TransactionTooLargeException:数据宗地大小为10722432字节

这是我的MainActivity.java

package de.lucbe.tictactoe;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button[] buttons = new Button[9];

int turn = 0;

boolean isTurnCircle = false;

int i;

boolean gameIsRunning = true;

TextView textView;

TicTacToe ttt = new TicTacToe();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.textView);

    for(i = 0;i < buttons.length; i++){
        String buttonID = "button" + i;
        int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
        buttons[i] = ((Button) findViewById(resID));

        buttons[i].setOnClickListener(this);
    }

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button0:
            ttt.ticTacToe(0);

            break;
        case R.id.button1:
            ttt.ticTacToe(1);

            break;
        case R.id.button2:
            ttt.ticTacToe(2);

            break;
        case R.id.button3:
            ttt.ticTacToe(3);

            break;
        case R.id.button4:
            ttt.ticTacToe(4);

            break;
        case R.id.button5:
            ttt.ticTacToe(5);

            break;
        case R.id.button6:
            ttt.ticTacToe(6);

            break;
        case R.id.button7:
            ttt.ticTacToe(7);

            break;
        case R.id.button8:
            ttt.ticTacToe(8);

            break;
    }
}
}

这是我的TicTacToe.java

 package de.lucbe.tictactoe;

import android.widget.Button;
import android.widget.TextView;

public class TicTacToe {

MainActivity MA = new MainActivity();

boolean gameIsRunning = MA.gameIsRunning;
boolean isTurnCircle = MA.isTurnCircle;
int turn = MA.turn;
Button[] buttons = MA.buttons;
TextView textView = MA.textView;


public void ticTacToe(int id){

    if(gameIsRunning){

        if(turn < 9){

            if(isTurnCircle){
                buttons[id].setText("o");
                isTurnCircle = false;
            }else if(!isTurnCircle){
                buttons[id].setText("x");
                isTurnCircle = true;
            }

            turn++;

            buttons[id].setEnabled(false);

        }

        if(buttons[0].getText() == "x" && buttons[1].getText() == "x" && buttons[2].getText() == "x"){
            textView.setText("x hat gewonnen");
            gameIsRunning = false;
        }else if(buttons[3].getText() == "x" && buttons[4].getText() == "x" && buttons[5].getText() == "x"){
            textView.setText("x hat gewonnen");
            gameIsRunning = false;
        }else if(buttons[6].getText() == "x" && buttons[7].getText() == "x" && buttons[8].getText() == "x"){
            textView.setText("x hat gewonnen");
            gameIsRunning = false;
        }else if(buttons[0].getText() == "x" && buttons[3].getText() == "x" && buttons[6].getText() == "x"){
            textView.setText("x hat gewonnen");
            gameIsRunning = false;
        }else if(buttons[1].getText() == "x" && buttons[4].getText() == "x" && buttons[7].getText() == "x"){
            textView.setText("x hat gewonnen");
            gameIsRunning = false;
        }else if(buttons[2].getText() == "x" && buttons[5].getText() == "x" && buttons[8].getText() == "x"){
            textView.setText("x hat gewonnen");
            gameIsRunning = false;
        }else if(buttons[0].getText() == "x" && buttons[4].getText() == "x" && buttons[8].getText() == "x"){
            textView.setText("x hat gewonnen");
            gameIsRunning = false;
        }else if(buttons[2].getText() == "x" && buttons[4].getText() == "x" && buttons[6].getText() == "x"){
            textView.setText("x hat gewonnen");
            gameIsRunning = false;
        }



        if(buttons[0].getText() == "o" && buttons[1].getText() == "o" && buttons[2].getText() == "o"){
            textView.setText("o hat gewonnen");
            gameIsRunning = false;
        }else if(buttons[3].getText() == "o" && buttons[4].getText() == "o" && buttons[5].getText() == "o"){
            textView.setText("o hat gewonnen");
            gameIsRunning = false;
        }else if(buttons[6].getText() == "o" && buttons[7].getText() == "o" && buttons[8].getText() == "o"){
            textView.setText("o hat gewonnen");
            gameIsRunning = false;
        }else if(buttons[0].getText() == "o" && buttons[3].getText() == "o" && buttons[6].getText() == "o"){
            textView.setText("o hat gewonnen");
            gameIsRunning = false;
        }else if(buttons[1].getText() == "o" && buttons[4].getText() == "o" && buttons[7].getText() == "o"){
            textView.setText("o hat gewonnen");
            gameIsRunning = false;
        }else if(buttons[2].getText() == "o" && buttons[5].getText() == "o" && buttons[8].getText() == "o"){
            textView.setText("o hat gewonnen");
            gameIsRunning = false;
        }else if(buttons[0].getText() == "o" && buttons[4].getText() == "o" && buttons[8].getText() == "o"){
            textView.setText("o hat gewonnen");
            gameIsRunning = false;
        }else if(buttons[2].getText() == "o" && buttons[4].getText() == "o" && buttons[6].getText() == "o"){
            textView.setText("o hat gewonnen");
            gameIsRunning = false;
        }



        if(turn == 9){
            textView.setText("Game over!");
        }

    }

}

}

提前谢谢。抱歉这个很长的问题。

1 个答案:

答案 0 :(得分:1)

我的第一印象是问题来自

MainActivity MA = new MainActivity();

这不是将信息从活动传递到类的常用方法。这会为您的活动创建一个新实例,只要您不在MainActivity中使用静态字段,我就不会认为这应该有用(使用静态字段虽然这里不是一个坏主意)。

您应该使用所需参数调用TicTacToe类。