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問題はシリアル化できないことだと思います。オブジェクトのシリアル化に問題がある人からのSEに関する質問がたくさんありLocationます。ただし、Locationですので、使用するのではなく、クラスにParcelable実装するだけで幸運が得られるかもしれません。ParcelableSerializable

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

}

理由はわかりませんが、トラックをアクティビティに通している間、新しいインテントが開始される前にチェックしたにもかかわらず、ルートリストに要素が1つしかなく、完全なリストがあります。

于 2012-09-05T17:14:16.073 に答える
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 に答える
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 に答える