gpt4 book ai didi

Android ArrayList 在 Activity 之间传递

转载 作者:行者123 更新时间:2023-11-30 04:02:33 27 4
gpt4 key购买 nike

我有一个简单的类 Track,它存储有关路线的信息:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;

import android.location.Location;

public class Track implements Serializable {
private static final long serialVersionUID = -5317697499269650204L;

private Date date;
private String name;
private int time;
private double distance, speed;
private ArrayList<Location> route;

public Track(String name, int time, double distance, ArrayList<Location> route) {
this.date = new Date();
this.name = name;
this.time = time;
this.distance = distance;
this.speed = distance / (time / 3600.);
this.route = route;
}

public String getDate() {
return String.format("Date: %1$td-%1$tb-%1$tY%nTime: %1$tH:%1$tM:%1$tS", date);
}

public String getName() {
return name;
}

public int getTime() {
return time;
}

public double getDistance() {
return distance;
}

public float getSpeed() {
return (float) speed;
}

public ArrayList<Location> getRoute() {
return route;
}

@Override
public String toString() {
return String.format("Name: %s%nDate: %2$td-%2$tb-%2$tY%nTime: %2$tH:%2$tM:%2$tS", name, date);
}
}

我将它从一个 Activity 传递到另一个 Activity :

Intent showTrackIntent = new Intent(TabSavedActivity.this, ShowTrackActivity.class);
showTrackIntent.putExtra("track", adapter.getItem(position));
startActivity(showTrackIntent);

在哪里(Track 对象是 ListView 上的元素)。我在传递 Track 对象时出错:

java.lang.RuntimeException: Parcelable 遇到 IOException 写入可序列化对象 (name = classes.Track)

发生了什么事?

最佳答案

我认为问题在于 Location 不可序列化。尝试序列化 Location 对象时遇到问题的人对 SE 提出了很多问题。但是,LocationParcelable,所以也许您只为您的类实现 Parcelable 而不是使用 Serializable

关于Android ArrayList<Location> 在 Activity 之间传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12269944/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com