我有关于将对象发送到其他活动的问题。我不知道这是怎么做的。所以,我在MainActivity将对象传递给其他活动
final Player player = new Player("Player", 150);
我对球员单独的类简单的构造
public class Player {
private String playerName;
private double playerCash;
Player(String playerName, double playerCash)
{
this.playerName = playerName;
this.playerCash = playerCash;
}
对象的球员,我有第二个活动,在这里我想用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对象,并希望在多个活动中使用它,但不知道如何。对于任何帮助,非常感谢;)
您无法将意图传递给putExtra中的对象。布尔,int,字符串等是允许的。 – drWisdom
您必须在您的类上实现Serializable并将其作为可序列化移动 –
您的对象需要实现Serializable或Parcelable接口 –