1

我有简单的 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);
    }
}

我将它从一项活动传递到另一项活动:

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

4 回答 4

1

我认为问题在于Location不可序列化。尝试序列化Location对象时遇到问题的人提出了很多关于 SE 的问题。但是,Locationis Parcelable,所以也许你会更好地Parcelable为你的班级实施而不是使用Serializable

于 2012-09-04T21:36:10.360 回答
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 时,我在路由列表中只得到一个元素,即使我在新意图开始之前检查了它并且有完整列表。

于 2012-09-05T17:14:16.073 回答
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");
于 2012-09-07T14:00:38.217 回答
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;
}
}

大卫瓦瑟你非常有帮助,谢谢!

于 2012-09-06T16:23:48.170 回答