You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

安卓开发:Intent传递Serializable对象遇空指针异常求助

问题解决思路

1. 修复模型类的序列化问题

你用getSerializable()传递PlayerModel对象,但你的PlayerModel类没有实现Serializable接口,这会导致对象无法被正确序列化传递。修改模型类:

public class PlayerModel implements Serializable { // 新增实现Serializable接口
    private int id;
    private String playerName;
    private String playerBirthday;
    private String playerClub;

    // 原有getter、setter、构造函数保持不变
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPlayerName() {
        return playerName;
    }

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

    public String getPlayerBirthday() {
        return playerBirthday;
    }

    public void setPlayerBirthday(String playerBirthday) {
        this.playerBirthday = playerBirthday;
    }

    public String getPlayerClub() {
        return playerClub;
    }

    public void setPlayerClub(String playerClub) {
        this.playerClub = playerClub;
    }

    public PlayerModel(int id, String playerName, String playerBirthday, String playerClub){
        this.id = id;
        this.playerName = playerName;
        this.playerBirthday = playerBirthday;
        this.playerClub = playerClub;
    }

    public PlayerModel(String playerName, String playerBirthday, String playerClub) {
        this.playerName = playerName;
        this.playerBirthday = playerBirthday;
        this.playerClub = playerClub;
    }
}

2. 避免空指针异常

MainActivity可能不是仅从PlayerList跳转而来(比如直接从桌面启动),此时getIntent().getExtras()会返回null,直接调用getSerializable()就会触发空指针。必须增加空判断:

// 替换MainActivity中获取对象的代码
Intent intent = getIntent();
if (intent != null && intent.getExtras() != null) {
    PlayerModel playerModel = (PlayerModel) intent.getExtras().getSerializable("PLAYER");
    if (playerModel != null) {
        tvMainPN.setText(playerModel.getPlayerName());
        tvMainPB.setText(playerModel.getPlayerBirthday());
        tvMainPC.setText(playerModel.getPlayerClub());
    }
}

3. (可选)改用Parcelable提升传递效率

安卓官方更推荐使用Parcelable接口传递对象,性能比Serializable更高,实现方式如下:

修改PlayerModel实现Parcelable

public class PlayerModel implements Parcelable {
    private int id;
    private String playerName;
    private String playerBirthday;
    private String playerClub;

    // 原有构造函数
    public PlayerModel(int id, String playerName, String playerBirthday, String playerClub){
        this.id = id;
        this.playerName = playerName;
        this.playerBirthday = playerBirthday;
        this.playerClub = playerClub;
    }

    public PlayerModel(String playerName, String playerBirthday, String playerClub) {
        this.playerName = playerName;
        this.playerBirthday = playerBirthday;
        this.playerClub = playerClub;
    }

    // Parcelable核心实现
    protected PlayerModel(Parcel in) {
        id = in.readInt();
        playerName = in.readString();
        playerBirthday = in.readString();
        playerClub = in.readString();
    }

    public static final Creator<PlayerModel> CREATOR = new Creator<PlayerModel>() {
        @Override
        public PlayerModel createFromParcel(Parcel in) {
            return new PlayerModel(in);
        }

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

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(playerName);
        dest.writeString(playerBirthday);
        dest.writeString(playerClub);
    }

    // 原有getter、setter保持不变
    public int getId() { return id; }
    public void setId(int id) { this.id = id; }
    public String getPlayerName() { return playerName; }
    public void setPlayerName(String playerName) { this.playerName = playerName; }
    public String getPlayerBirthday() { return playerBirthday; }
    public void setPlayerBirthday(String playerBirthday) { this.playerBirthday = playerBirthday; }
    public String getPlayerClub() { return playerClub; }
    public void setPlayerClub(String playerClub) { this.playerClub = playerClub; }
}

修改传递与获取逻辑

  • PlayerList中传递对象代码不变:
intent.putExtra("PLAYER", playerModel);
  • MainActivity中获取对象:
Intent intent = getIntent();
if (intent != null && intent.hasExtra("PLAYER")) {
    PlayerModel playerModel = intent.getParcelableExtra("PLAYER");
    if (playerModel != null) {
        tvMainPN.setText(playerModel.getPlayerName());
        tvMainPB.setText(playerModel.getPlayerBirthday());
        tvMainPC.setText(playerModel.getPlayerClub());
    }
}

额外验证点

确认playerDb.getPlayerData()返回的arrayList不为空,且onItemClick中能拿到有效对象,可加日志验证:

// 在PlayerList的onItemClick中添加日志
PlayerModel playerModel=arrayList.get(position);
Log.d("PlayerList", "传递的球员名称: " + playerModel.getPlayerName());

内容的提问来源于stack exchange,提问作者Gamat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 03:27:08