Parcelable ArrayList <latlng> Android </latlng>

时间:2013-03-14 13:55:41

标签: android parcelable

我不能在我的对象属性“ArrayList”上实现Parcelable,显然我的属性“ArrayList工作,为什么?因为,我使用相同的方法:

in.readTypedList(etapes,Step.CREATOR);          
in.readTypedList(points, LatLng.CREATOR);//BUG      

我的课程,当然还有:“导入com.google.android.gms.maps.model.LatLng;”

public class Road implements Parcelable {
    public String depart;
    public String arrivee;
    public int distance;
    public int duration;
    public ArrayList<Step> etapes = new ArrayList<Step>();
    public String travel_mode;
    public LatLngBounds bounds;    
    public ArrayList<LatLng> points = new ArrayList<LatLng>();

    public Road(String depart, String arrivee, int distance, int duration, ArrayList<Step> etapes, String travel_mode, ArrayList<LatLng> points){

        this.depart = depart;
        this.arrivee = arrivee;
        this.distance = distance;
        this.duration = duration;
        this.etapes = etapes;
        this.travel_mode = travel_mode;                 

        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        for(int i=0; i<points.size();i++){              
            builder.include(points.get(i));             
        }
        bounds = builder.build();
        this.points = points;  

    }        

    private Road(Parcel in){
        depart =  in.readString();
        arrivee = in.readString();
        distance = in.readInt();
        duration = in.readInt(); 
        in.readTypedList(etapes,Step.CREATOR);//GOOD            
        travel_mode = in.readString();
        LatLng ne = new LatLng(in.readDouble(),in.readDouble());
        LatLng sw = new LatLng(in.readDouble(),in.readDouble());
        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        builder.include(ne);
        builder.include(sw);
        bounds = builder.build();           
        in.readTypedList(points, LatLng.CREATOR);//BUG      
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub          
        dest.writeString(depart);
        dest.writeString(arrivee);
        dest.writeInt(distance);
        dest.writeInt(duration);            
        dest.writeTypedList(etapes);            
        dest.writeString(travel_mode);
        dest.writeDouble(bounds.northeast.latitude);
        dest.writeDouble(bounds.northeast.longitude);
        dest.writeDouble(bounds.southwest.latitude);
        dest.writeDouble(bounds.southwest.longitude);           
        dest.writeParcelable(bounds, flags);
        dest.writeTypedList(points);            
    }       

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

        public Road createFromParcel(Parcel in){
            return new Road(in);
        }
        public Road[] newArray(int size){
            return new Road[size];          
        }
    };      

}

我的stacktrace:

03-14 14:34:12.431: E/AndroidRuntime(9809): FATAL EXCEPTION: main
03-14 14:34:12.431: E/AndroidRuntime(9809): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.TEST flg=0x10 (has extras) } in com.eloges.GeoPosActivity$2@41013368
03-14 14:34:12.431: E/AndroidRuntime(9809):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:794)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at android.os.Handler.handleCallback(Handler.java:608)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at android.os.Looper.loop(Looper.java:156)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at android.app.ActivityThread.main(ActivityThread.java:5099)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at java.lang.reflect.Method.invokeNative(Native Method)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at java.lang.reflect.Method.invoke(Method.java:511)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at dalvik.system.NativeStart.main(Native Method)
03-14 14:34:12.431: E/AndroidRuntime(9809): Caused by: com.google.android.gms.internal.aw$a: Expected object header. Got 0x2e006d Parcel: pos=3484 size=17768
03-14 14:34:12.431: E/AndroidRuntime(9809):     at com.google.android.gms.internal.aw.c(Unknown Source)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at com.google.android.gms.maps.model.LatLngCreator.createFromParcel(Unknown Source)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at com.google.android.gms.maps.model.LatLngCreator.createFromParcel(Unknown Source)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at android.os.Parcel.readTypedList(Parcel.java:1655)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at com.eloges.geoloc.Road.<init>(Road.java:66)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at com.eloges.geoloc.Road.<init>(Road.java:49)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at com.eloges.geoloc.Road$1.createFromParcel(Road.java:98)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at com.eloges.geoloc.Road$1.createFromParcel(Road.java:1)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at android.os.Parcel.readParcelable(Parcel.java:1997)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at android.os.Parcel.readValue(Parcel.java:1859)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at android.os.Parcel.readMapInternal(Parcel.java:2099)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at android.os.Bundle.unparcel(Bundle.java:223)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at android.os.Bundle.getParcelable(Bundle.java:1158)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at com.eloges.GeoPosActivity$2.onReceive(GeoPosActivity.java:162)
03-14 14:34:12.431: E/AndroidRuntime(9809):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:781)
03-14 14:34:12.431: E/AndroidRuntime(9809):     ... 9 more

1 个答案:

答案 0 :(得分:0)

对ArrayLists使用writeParcelableArray(),并确保您的对象LatLng和Step是Parcelable to。