FeelsBard Main FeelsBard Main - 1 month ago 13
Android Question

Java error, App crashes, (parcel size = 10722432)

I am currently making a TicTacToe app and since one of my functions had become pretty huge i decided to put it in another class. The app worked before but now when I want to start the app on my phone it crashes immediately. This is the error i get:

Error reporting crash
android.os.

TransactionTooLargeException: data parcel size 10722432 bytes

Here is my 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;
}
}
}


And this is my 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!");
}

}

}


}

Thx in advance. And sorry for the very long question.

Answer

my first impression is that the problem comes from

MainActivity MA = new MainActivity();

this is not a common way to pass information from an activity to a class. This makes a new instance of your activity and as long as you don't use static fields in your MainActivity, i don't think this should work (using static fields is although a bad idea here).

You should call the TicTacToe class over methods with the needed arguments..

Comments