将多个对象传递给另一个活动

时间:2017-11-05 14:20:15

标签: java android android-intent android-activity parcelable

我正在尝试创建一个非常基本的应用程序,它基本上跟踪在现实生活中玩纸牌游戏的4名玩家的得分。我的想法是为每个玩家创建一个“玩家”类的实例,现在只包含变量“name”和“score”。我按照本文的说明(http://sohailaziz05.blogspot.de/2012/04/passing-custom-objects-between-android.html)并在“播放器”类中实现了Parcelable:

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

}

这是第一个活动的代码(我这里只创建一个玩家,理想情况下应该是4个或更多):

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

当我在第一个活动中按下按钮时,将触发“startGame”并且玩家姓名和分数应该显示在第二个活动中:

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

所以当我运行它时,一旦按下按钮从第一个活动到第二个活动,我在模拟器中收到以下错误消息: 游戏停止了 再次打开应用

出了什么问题?是parcelable正确的方法还是我应该使用serializable?我怎样才能将第1个,第4个物体传递给第二个物体?

我很感激你的帮助,我真的被困在这里......谢谢!

1 个答案:

答案 0 :(得分:1)

这就是我通常的做法。

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