将对象传递给其他活动

时间:2016-02-16 17:31:27

标签: android

我对将对象发送到其他活动有疑问。我不确定这是我在做什么。所以我在MainActivity中有对象播放器

final Player player = new Player("Player", 150);

我有一个单独的Player类,带有简单的构造函数

public class Player {

private String playerName;
private double playerCash;

Player(String playerName, double playerCash)
{
    this.playerName = playerName;
    this.playerCash = playerCash;
}

我有第二个Activity,我想要使用Player对象。我使用此代码在MainActivity中创建了一个按钮

 mButton = (Button) findViewById(R.id.mButton);
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            intent.putExtra("player", player);
            startActivity(intent);
        }
    });

现在我遇到问题“无法解决方法putExtra”。我究竟做错了什么?我只想要一个Player对象,并希望在多个活动中使用它,但不知道如何。对于任何帮助,非常感谢;)

4 个答案:

答案 0 :(得分:1)

上述答案中提到的所有内容都非常清楚地描述了解决方案。 这是代码:

public class Player implements Parcelable{
private String playerName;
private double playerCash;

    // Constructor
    public Player(String playerName, double playerCash){
        this.playerName = playerName;
        this.playerCash = playerCash;
   }
   // Implement Getter and setter methods


   // Parcelling part
   public Player(Parcel in){
       this.playerName = in.readString();
       this.playerCash = in.readDouble();
   }

   @Оverride
   public int describeContents(){
       return 0;
   }

   @Override
   public void writeToParcel(Parcel dest, int flags) {
      dest.writeString(playerName);
      dest.writeDouble(playerCash);
   }
   public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
       public Player createFromParcel(Parcel in) {
           return new Player(in); 
       }

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

答案 1 :(得分:0)

传递自定义对象有点复杂。您可以将该类标记为Serializable,让Java处理此问题。

然而,在Android上,使用Serializable会带来严重的性能损失。解决方案是使用Parcelable。请点击此链接了解实施细节:http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/

答案 2 :(得分:0)

你可以使用parcelable,看看我的帖子: https://stackoverflow.com/a/35252575/982161

因为您需要对播放器类进行一些更改。

答案 3 :(得分:0)

制作一个Serializable类

import java.io.Serializable;

@SuppressWarnings( “串行”) 公共类MyPlayer实现Serializable {

private String playerName; private double playerCash;

public MyPlayer(String playerName, double playerCash) {
    this.playerName = playerName;
    this.playerCash = playerCash;
}

public String getPlayerName() {
    return playerName;
}

public void setPlayerName(String playerName) {
    this.playerName = playerName;
}

public double getPlayerCash() {
    return this.playerCash;
}

public void setPlayerCash(double playerCash) {
    this.playerCash = playerCash;
}

}

然后在你的按钮点击

mButton = (Button) findViewById(R.id.mButton);
mButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        MyPlayer myPlayer = new MyPlayer(playerName,playerCash);
        Intent i = new Intent(MainActivity.this, SecondActivity.class);
        i.putExtra("key", myPlayer);
        startActivity(i);
    }
});

使用传递数据(在第二个活动中)

Intent i = getIntent();

MyPlayer myPlayer =(MyPlayer)i.getSerializableExtra(“key”);