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

如何通过putExtra()在Activity间传递对象?Parcelable+GeoPoint遇阻求助

解决Station类结合GeoPoint实现Parcelable的问题

我来帮你搞定这个Parcelable结合GeoPoint的问题!你的现有代码存在几个关键问题:比如Parcel构造函数里没正确初始化location字段(直接调用location.getLongitude()会导致空指针),还缺少了writeToParcel方法的实现,以及完整的describeContents和CREATOR内部类。

下面是修正后的完整代码,我会标注关键要点:

import android.os.Parcel;
import android.os.Parcelable;
// 请导入你实际使用的GeoPoint包,比如谷歌地图库的GeoPoint
import com.google.android.gms.maps.model.GeoPoint;

public class Station implements Parcelable {
    private String description;
    private String linenumber;
    private GeoPoint location;
    private String name;

    // 常规构造函数(按需添加)
    public Station(String description, String linenumber, GeoPoint location, String name) {
        this.description = description;
        this.linenumber = linenumber;
        this.location = location;
        this.name = name;
    }

    // Parcel构造函数:从Parcel中读取数据
    protected Station(Parcel in) {
        this.description = in.readString();
        this.name = in.readString();
        this.linenumber = in.readString();
        // 关键:读取Parcelable类型的GeoPoint,传入对应类加载器
        this.location = in.readParcelable(GeoPoint.class.getClassLoader());
    }

    @Override
    public int describeContents() {
        // 除非类包含特殊文件描述符,否则返回0即可
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // 写入顺序必须和构造函数的读取顺序完全一致
        dest.writeString(description);
        dest.writeString(name);
        dest.writeString(linenumber);
        // 关键:写入Parcelable类型的GeoPoint
        dest.writeParcelable(location, flags);
    }

    // 必须实现的CREATOR内部类,用于从Parcel生成Station实例
    public static final Creator<Station> CREATOR = new Creator<Station>() {
        @Override
        public Station createFromParcel(Parcel in) {
            return new Station(in);
        }

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

    // 可选:添加getter方法方便外部获取字段
    public String getDescription() {
        return description;
    }

    public String getLinenumber() {
        return linenumber;
    }

    public GeoPoint getLocation() {
        return location;
    }

    public String getName() {
        return name;
    }
}

关键注意事项:

  • GeoPoint的读写逻辑:因为GeoPoint本身实现了Parcelable接口(主流Android地图库的GeoPoint都支持),所以直接用readParcelable和writeParcelable就能完成序列化,不需要手动拆分经纬度。
  • 字段顺序一致性:writeToParcel的写入顺序,必须和Station(Parcel in)构造函数的读取顺序完全匹配,否则会出现数据错乱。
  • 空值兼容(可选):如果location可能为null,可以添加额外的空值判断逻辑:
    // 写入时
    if (location != null) {
        dest.writeInt(1);
        dest.writeParcelable(location, flags);
    } else {
        dest.writeInt(0);
    }
    // 读取时
    int hasLocation = in.readInt();
    if (hasLocation == 1) {
        location = in.readParcelable(GeoPoint.class.getClassLoader());
    }
    
  • 类加载器正确性:读取Parcelable时必须传入对应类的类加载器,这里用GeoPoint.class.getClassLoader()即可。

完成后,你就可以在组件间正常传递Station对象了,比如Activity之间:

// 发送端
Station station = new Station("市中心站点", "2号线", new GeoPoint(399048, 1164073), "人民广场站");
Intent intent = new Intent(this, TargetActivity.class);
intent.putExtra("selected_station", station);
startActivity(intent);

// 接收端
Station receivedStation = getIntent().getParcelableExtra("selected_station");

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:16:14