扩展android Location类并实现parcelable接口

时间:2013-12-13 14:41:28

标签: android location extends parcelable

我有一个Coordinate类,它扩展了实现Parcelable的android.location.Location类。

这是我的坐标课:

public class Coordinate extends Location {


public Coordinate() {
    super(Keys.DUMMY_PROVIDER);
}

public Coordinate(Double longitude, Double latitude, Double altitude) {
    super(Keys.DUMMY_PROVIDER);
    this.setLongitude(longitude);
    this.setLatitude(latitude);
    this.setAltitude(altitude);
}

public Coordinate(Double longitude, Double latitude, Double altitude, Float speed, Long time) {
    super(Keys.DUMMY_PROVIDER);
    this.setLongitude(longitude);
    this.setLatitude(latitude);
    this.setAltitude(altitude);
    this.setSpeed(speed);
    this.setTime(time);
}

public Coordinate(String raw) {
    super(Keys.DUMMY_PROVIDER);
    String[] coords;
    coords = raw.split("(?<!\\\\),");
    this.setLongitude(Double.parseDouble(coords[0]));
    this.setLatitude(Double.parseDouble(coords[1]));
    this.setAltitude(coords.length>2? Double.parseDouble(coords[2]) : null);
}

public Coordinate(Location location) {
    super(location);
}

public static final Parcelable.Creator<Coordinate> CREATOR = new Parcelable.Creator<Coordinate>() {
    public Coordinate createFromParcel(Parcel in) {
        return new Coordinate(in);
    }

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

private Coordinate(Parcel in) {
    super(Keys.DUMMY_PROVIDER);
    in.readParcelable(Location.class.getClassLoader());
} 

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    super.writeToParcel(dest, flags);
}
}

当我运行我的代码时出现此错误:

    12-13 15:21:29.595: E/AndroidRuntime(5499): FATAL EXCEPTION: main
    12-13 15:21:29.595: E/AndroidRuntime(5499): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lavachequiskie.sanbernardo/com.lavachequiskie.sanbernardo.socialnetwork.SocialNetworkActivity}: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: dummy-provider
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2107)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2132)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at android.os.Handler.dispatchMessage(Handler.java:99)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at android.os.Looper.loop(Looper.java:137)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at android.app.ActivityThread.main(ActivityThread.java:4918)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at java.lang.reflect.Method.invokeNative(Native Method)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at java.lang.reflect.Method.invoke(Method.java:511)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at dalvik.system.NativeStart.main(Native Method)
    12-13 15:21:29.595: E/AndroidRuntime(5499): Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: dummy-provider
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at android.os.Parcel.readParcelable(Parcel.java:2077)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at com.lavachequiskie.mykml.Coordinate.<init>(Coordinate.java:79)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at com.lavachequiskie.mykml.Coordinate.<init>(Coordinate.java:77)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at com.lavachequiskie.mykml.Coordinate$1.createFromParcel(Coordinate.java:69)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at com.lavachequiskie.mykml.Coordinate$1.createFromParcel(Coordinate.java:1)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at android.os.Parcel.readParcelable(Parcel.java:2103)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at com.lavachequiskie.mykml.SimplifiedPlacemark.<init>(SimplifiedPlacemark.java:21)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at com.lavachequiskie.mykml.SimplifiedPlacemark.<init>(SimplifiedPlacemark.java:17)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at com.lavachequiskie.mykml.SimplifiedPlacemark$1.createFromParcel(SimplifiedPlacemark.java:89)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at com.lavachequiskie.mykml.SimplifiedPlacemark$1.createFromParcel(SimplifiedPlacemark.java:1)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at android.os.Parcel.readParcelable(Parcel.java:2103)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at android.os.Parcel.readValue(Parcel.java:1965)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at android.os.Parcel.readMapInternal(Parcel.java:2226)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at android.os.Bundle.unparcel(Bundle.java:223)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at android.os.Bundle.getString(Bundle.java:1055)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at com.lavachequiskie.sanbernardo.socialnetwork.SocialNetworkActivity.onCreate(SocialNetworkActivity.java:104)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at android.app.Activity.performCreate(Activity.java:5185)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
    12-13 15:21:29.595: E/AndroidRuntime(5499):     ... 11 more

我认为我的问题来自构造函数private Coordinate(Parcel in),但是Location类没有super(Parcel in)构造函数。我发现了一些关于这个特定问题的帖子(firtsecond),但最后我不明白是否可以这样做以及如何做?

0 个答案:

没有答案