简体   繁体   中英

Passing multiple objects to another activity

I am trying to create a very basic app which basically keeps track of the scores of 4 players who play a card game in real life. My idea was to create an instance of the class "Player" for each player, which for now only contains the variables "name" and "score". I followed the instructions of this article ( http://sohailaziz05.blogspot.de/2012/04/passing-custom-objects-between-android.html ) and implemented Parcelable in the "Player" class:

public class Player implements Parcelable {
    private String name;
    private int score;

    public Player(String name, int score) {
        this.name = name;
        this.score = score;
    }

    public String getPlayerName() {
        return name;
    }

    public int getPlayerScore() {
        return score;
    }

    public Player(Parcel in) {
        String[] data = new String[2];

        in.readStringArray(data);
        this.name = data[0];
        this.score = Integer.parseInt(data[1]);

    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeStringArray(new String[]{this.name, String.valueOf(this.score)});
    }

    public static final Parcelable.Creator<Player> CREATOR = new Parcelable.Creator<Player>() {

        @Override
        public Player createFromParcel(Parcel source) {
            return new Player(source);  //using parcelable constructor
        }

        @Override
        public Player[] newArray(int size) {
            return new Player[size];
        }
    };

}

This is the code for the first activity (I am only creating one player here, ideally it should be 4 or more):

public class CreatePlayersScreen extends AppCompatActivity {

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

    public void startGame(View view) {

        EditText editText1 = (EditText) findViewById(R.id.editText1);
        String namePlayer1 = editText1.getText().toString();
        int scorePlayer1=0;
        Player player1 = new Player(namePlayer1, scorePlayer1);

        Intent intent=new Intent(this,ScoreScreen.class);
        intent.putExtra("EXTRA_PLAYER_1",player1);

        startActivity(intent);
    }    
}

When I press a button in this first activity, "startGame" will be triggered and player names and scores are supposed to be shown in the second activity:

public class ScoreScreen extends AppCompatActivity {

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

        Player player1= getIntent().getParcelableExtra("EXTRA_PLAYER_1");

        String namePlayer1 = player1.getPlayerName();
        TextView textView1 = (TextView) findViewById(R.id.textView1);
        textView1.setText(namePlayer1);

        int scorePlayer1 = player1.getPlayerScore();
        TextView textView5 = (TextView) findViewById(R.id.textView5);
        textView5.setText(scorePlayer1);
    }
}

So when I run this, I get the following error message in the emulator once I press the button to get from the first to the second activity: Game has stopped Open app again

What is going wrong? Is parcelable the right approach or should i use serializable? And how can I pass not only 1, but 4 objects to the second acitivity?

I'd appreciate your help, I'm really stuck here... Thanks!

This is how I typically do it.

public void startGame(View view) {
    EditText editText1 = (EditText) findViewById(R.id.editText1);
    String namePlayer1 = editText1.getText().toString();
    int scorePlayer1 = 0;
    Player player1 = new Player(namePlayer1, scorePlayer1);
    EditText editText2 = (EditText) findViewById(R.id.editText2);
    String namePlayer2 = editText2.getText().toString();
    int scorePlayer2 = 0;
    Player player2 = new Player(namePlayer2, scorePlayer2);

    Intent intent = new Intent(this, ScoreScreen.class);
    Bundle bundle = new Bundle();
    bundle.putParcelable("EXTRA_PLAYER_1", player1);
    bundle.putParcelable("EXTRA_PLAYER_2", player2);
    intent.putExtras(bundle);
    startActivity(intent);
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    Player player1;
    Player player2;
    if (getIntent().getExtras() != null) {
        player1 = getIntent().getExtras().getParcelable("EXTRA_PLAYER_1");
        player2 = getIntent().getExtras().getParcelable("EXTRA_PLAYER_2");
    }
}

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