Android ArrayList <location>在活动之间传递</location>

时间:2012-09-04 19:17:34

标签: android location serializable parcelable

我有简单的类Track,它存储有关route的信息:

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);
    }
}

我将它从一个活动转移到另一个活动:

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

其中(Track对象是ListView上的元素)。 我在传递Track对象时遇到错误:

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = classes.Track)

发生了什么事?

4 个答案:

答案 0 :(得分:1)

我认为问题是Location不可序列化。对于尝试序列化Location对象时遇到问题的人来说,有很多关于SE的问题。但是,LocationParcelable,所以也许您可以更好地为您的班级实施Parcelable而不是使用Serializable

答案 1 :(得分:0)

目前的代码是:

包类;

import java.util.ArrayList;
import java.util.Date;

import android.location.Location;
import android.os.Parcel;
import android.os.Parcelable;

public class Track implements Parcelable {
    private String name, date;
    private int time;
    private double distance, speed;
    private ArrayList<Location> route;

public Track(String name, int time, double distance, Date date, ArrayList<Location> route) {
    this.name = name;
    this.date = formatDate(date);
    this.time = time;
    this.distance = distance;
    this.route = route;

    this.speed = distance / (time / 3600.);
}

public void writeToParcel(Parcel dest, int flags) {
    dest.writeStringArray(new String[] { name, date });
    dest.writeInt(time);
    dest.writeDouble(distance);
    dest.writeSerializable(date);
    dest.writeTypedList(route);
}

private Track(Parcel in) {
    String[] strings = new String[2];
    in.readStringArray(strings);

    this.name = strings[0];
    this.date = strings[1];
    this.time = in.readInt();
    this.distance = in.readDouble();

    route = new ArrayList<Location>();
    in.readTypedList(route, Location.CREATOR);

    this.speed = distance / (time / 3600.);
}

public static final Parcelable.Creator<Track> CREATOR = new Parcelable.Creator<Track>() {

    public Track createFromParcel(Parcel in) {
        return new Track(in);
    }

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

public String getName() {
    return name;
}

public String getDate() {
    return date;
}

public int getTime() {
    return time;
}

public double getDistance() {
    return distance;
}

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

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

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

@Override
public String toString() {
    return String.format("Name: %s%nDate: %s", name, date);
}

public int describeContents() {
    return 0;
}

}

我不知道为什么但是当我通过活动传递Track时,我在路由列表中只有一个元素,即使我在新意图开始之前检查了它并且有完整列表。

答案 2 :(得分:0)

我认为该解决方案现已准备就绪。这是代码:

import java.util.ArrayList;
import java.util.Date;

import android.location.Location; 
import android.os.Parcel;
import android.os.Parcelable;

public class Track implements Parcelable {
private String name, date;
private int time;
private double distance, speed;
private ArrayList<Location> route;

public Track(String name, int time, double distance, Date date, ArrayList<Location> route) {
    this.name = name;
    this.date = formatDate(date);
    this.time = time;
    this.distance = distance;
    this.route = route;

    this.speed = distance / (time / 3600.);
}

public void writeToParcel(Parcel dest, int flags) {
    dest.writeStringArray(new String[] { name, date });
    dest.writeInt(time);
    dest.writeDouble(distance);
    dest.writeTypedList(route);
}

private Track(Parcel in) {
    String[] strings = new String[2];
    in.readStringArray(strings);
    this.name = strings[0];
    this.date = strings[1];

    this.time = in.readInt();
    this.distance = in.readDouble();

    route = new ArrayList<Location>();
    in.readTypedList(route, Location.CREATOR);

    this.speed = distance / (time / 3600.);
}

public static final Parcelable.Creator<Track> CREATOR = new Parcelable.Creator<Track>() {

    public Track createFromParcel(Parcel in) {
        return new Track(in);
    }

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

public String getName() {
    return name;
}

public String getDate() {
    return date;
}

public int getTime() {
    return time;
}

public double getDistance() {
    return distance;
}

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

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

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

@Override
public String toString() {
    return String.format("Name: %s%nDate: %s", name, date);
}

public int describeContents() {
    return 0;
}
}

David Wasser 你非常有帮助,谢谢!

答案 3 :(得分:0)

序列化:

    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(savedTracksFile));
    oos.writeObject(serializedTracks);
    oos.close();

反序列化:

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(savedTracksFile));
    serializedTracks = (ArrayList<Track>) ois.readObject();
    ois.close();

savedTracksFile的位置:

    File savedLocationsDirectory = new File(Environment.getExternalStorageDirectory().getPath().concat("/AppFolder"));
    if (!savedLocationsDirectory.exists()) savedLocationsDirectory.mkdirs();
    savedTracksFile = new File(savedLocationsDirectory, "savedTracks.gc");