使用parcelable在活动之间传递ArrayList对象

时间:2012-10-26 00:13:05

标签: java android android-intent arraylist parcelable

我首先开始这样做,说我已经看到所有其他帖子都处理了这个并尝试了所有这些。

我试图在其他帖子中做同样的事情,即使用Intent将ArrayList的值从一个活动传递到另一个活动。

我觉得我已正确实施了我的课程。我正在使用的主类(Route_Class)如下所示。

```

public class Route_Class implements Parcelable {
private double latitude = 0;
private double longitude = 0;
private double startingLat = 0;
private double startingLong = 0;
private String name = null;
private String routeName = null;
private String GeoPoints = null;
private String layout_width= null;
private String layout_height = null;
private String orientation = null;
private String xmlns = null;
private String id = null;
private boolean clickable = false;
private boolean enabled = false;
private String layout_width2 = null;
private String layout_height2 = null;
private String apiKey = null;
private ArrayList<GeoPoints_Class> geoPoints_arraylist = new ArrayList<GeoPoints_Class>();  

public Route_Class() {
    System.out.println("crash 110");
}

public Route_Class(String name, double latitude, double longitude, double startingLat, double startingLong, String routeName, String GeoPoints, String layout_width, String layout_height, String orientation, String xmlns, String id, boolean clickable, boolean enabled, String layout_width2, String layout_height2, String apiKey, ArrayList<GeoPoints_Class> geoPoints_arraylist) {
    this.name = name;
    this.latitude = latitude;
    this.longitude = longitude; 
    this.layout_width2 = layout_width2;
    this.latitude = latitude;
    this.longitude = longitude;
    this.startingLat = startingLat;
    this.startingLong = startingLong;   
    this.routeName = routeName;
    this.GeoPoints = GeoPoints;
    this.layout_width = layout_width;
    this.layout_height = layout_height;
    this.orientation = orientation;
    this.xmlns = xmlns;
    this.id = id;
    this.clickable = clickable;
    this.enabled = enabled;
    this.layout_height2 = layout_height2;
    this.apiKey = apiKey;
    this.geoPoints_arraylist = geoPoints_arraylist;
    System.out.println("crash 16");
}

 /* everything below here is for implementing Parcelable */

// 99.9% of the time you can just ignore this
public int describeContents() {
    System.out.println("crash 17");
    return this.hashCode();

}

// write your object's data to the passed-in Parcel
public void writeToParcel(Parcel dest, int flags) { 
    System.out.println("crash 18");
    dest.writeDouble(latitude);
    dest.writeDouble(longitude);
    dest.writeDouble(startingLat);
    dest.writeDouble(startingLong);
    dest.writeString(name);
    dest.writeString(routeName);
    dest.writeString(GeoPoints);
    dest.writeString(layout_width);
    dest.writeString(layout_height);
    dest.writeString(orientation);
    dest.writeString(xmlns);
    dest.writeString(id);
    dest.writeInt(clickable ? 0 : 1 );      
    dest.writeInt(enabled ? 0 : 1 );
    dest.writeString(layout_width2);
    dest.writeString(layout_height2);
    dest.writeString(apiKey);
    dest.writeTypedList(geoPoints_arraylist);
//      dest.writeList((List<GeoPoints_Class>)geoPoints_arraylist);

//      dest.writeParcelable((Parcelable) geoPoints_arraylist, 0);

}   

// this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
public static final Parcelable.Creator<Route_Class> CREATOR = new Parcelable.Creator<Route_Class>() {
    public Route_Class createFromParcel(Parcel in) {
        System.out.println("crash 20");
        return new Route_Class(in);
    }

    public Route_Class[] newArray(int size) {
        System.out.println("crash 21");
        return new Route_Class[size];
    }
};

// example constructor that takes a Parcel and gives you an object populated with it's values
private Route_Class(Parcel in) {
    layout_width2 = in.readString();
    latitude = in.readDouble();
    longitude = in.readDouble();
    startingLat = in.readDouble();
    startingLong = in.readDouble();
    name   = in.readString();
    routeName = in.readString();
    GeoPoints = in.readString();
    layout_width = in.readString();
    layout_height = in.readString();
    orientation = in.readString();
    xmlns = in.readString();
    id = in.readString();
    clickable = in.readInt() == 0;
    enabled = in.readInt() == 0;
    layout_height2 = in.readString();
    apiKey = in.readString();
    System.out.println("crash 5");
 //geoPoints_arraylist = new ArrayList<GeoPoints_Class>();
    if (geoPoints_arraylist == null) {
        geoPoints_arraylist = new ArrayList<GeoPoints_Class>();
    }
    try {
    in.readTypedList(geoPoints_arraylist, GeoPoints_Class.CREATOR);
    } catch (Exception e) {
        System.out.println("Error " + e);
    }
//        in.readList(geoPoints_arraylist,    com.breckbus.app.GeoPoints_Class.class.getClassLoader());  
    System.out.println("crash 6");

//        geoPoints_arraylist =  (ArrayList<GeoPoints_Class>)in.readParcelable(com.breckbus.app.Route_Class.GeoPoints_Class.  class.getClassLoader());
}

public double getlatitude() {
    return latitude;
}

public void setlatitude(double latitude) {
    this.latitude = latitude;
}

public double getlongitude() {
    return longitude;
}

public void setlongitude(double longitude) {
    this.longitude = longitude;
}

public double getstartingLat() {
    return startingLat;
}

public void setstartingLat(double startingLat) {
    this.startingLat = startingLat;
}

public double getstartingLong() {
    return startingLong;
}

public void setstartingLong(double startingLong) {
    this.startingLong = startingLong;
}

public String getname() {
    return name;
}

public void setname(String name) {
    this.name = name;
}

public String getrouteName() {
    return routeName;
}

public void setrouteName(String routeName) {
    this.routeName = routeName;
}

public String getGeoPoints() {
    return GeoPoints;
}

public void setGeoPoints(String GeoPoints) {
    this.GeoPoints = GeoPoints;
}

public String getLayout_width() {
    return layout_width;
}

public void setLayout_width(String layout_width) {
    this.layout_width = layout_width;
}

public String getLayout_height() {
    return layout_height;
}

public void setLayout_height(String layout_height) {
    this.layout_height = layout_height;
}

public boolean isEnabled() {
    return enabled;
}

public void setEnabled(boolean enabled) {
    this.enabled = enabled;
}

public String getOrientation() {
    return orientation;
}

public void setOrientation(String orientation) {
    this.orientation = orientation;
}

public String getXmlns() {
    return xmlns;
}

public void setXmlns(String xmlns) {
    this.xmlns = xmlns;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public boolean isClickable() {
    return clickable;
}

public void setClickable(boolean clickable) {
    this.clickable = clickable;
}

public String getLayout_width2() {
    return layout_width2;
}

public void setLayout_width2(String layout_width2) {
    this.layout_width2 = layout_width2;
}

public String getLayout_height2() {
    return layout_height2;
}

public void setLayout_height2(String layout_height2) {
    this.layout_height2 = layout_height2;
}

public String getApiKey() {
    return apiKey;
}

public void setApiKey(String apiKey) {
    this.apiKey = apiKey;
}
@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("Route Class Details:").append("\n\n");   
    sb.append("latitude: ").append(getlatitude());
    sb.append("\n");
    sb.append("longitude: ").append(getlongitude());
    sb.append("\n");
    sb.append("startingLat: ").append(getstartingLat());
    sb.append("\n");
    sb.append("startingLong: ").append(getstartingLong());
    sb.append("\n");
    sb.append("name: ").append(getname());
    sb.append("\n");
    sb.append("routeName: ").append(getrouteName());
    sb.append("\n");
    sb.append("GeoPoints: ").append(getGeoPoints());
    sb.append("\n");
    sb.append("layout_width: ").append(getLayout_width());
    sb.append("\n");
    sb.append("layout_height: ").append(getLayout_height());
    sb.append("\n");
    sb.append("orientation: ").append(getOrientation());
    sb.append("\n");
    sb.append("xmlns: ").append(getXmlns());
    sb.append("\n");
    sb.append("id: ").append(getId());
    sb.append("\n");
    sb.append("clickable: ").append(isClickable());
    sb.append("\n");
    sb.append("enabled: ").append(isEnabled());
    sb.append("\n");
    sb.append("layout_width2: ").append(layout_width2);
    sb.append("\n");
    sb.append("layout_height2: ").append(getLayout_height2());
    sb.append("\n");
    sb.append("apiKey: ").append(getApiKey());                        

    return sb.toString();
} 

//   public ArrayList<GeoPoints_Class> getGeoPoints_arraylist() {
//      return geoPoints_arraylist;
//  }
 public ArrayList<GeoPoints_Class> getGeoPoints_arraylist() {
        return geoPoints_arraylist;
    }

public void setGeoPoints_arraylist(ArrayList<GeoPoints_Class> geoPoints_arraylist)      {
    this.geoPoints_arraylist = geoPoints_arraylist;
}

public GeoPoints_Class getGeoPoints(int i) {
    return geoPoints_arraylist.get(i);
}       

public void addGeoPoint(double lat, double lon, String location) {
    this.geoPoints_arraylist.add(new GeoPoints_Class(lat, lon, location));
}

 }

```

这是我在Route_Class中使用的第二个类(GeoPoints_Class)。

```

package com.breckbus.app;

import android.os.Parcel;
import android.os.Parcelable;


class GeoPoints_Class implements Parcelable {

private double lat; 

private double lon; 

private String location;

public GeoPoints_Class(){
    System.out.println("crash 99");
}

public GeoPoints_Class(double lat, double lon, String location){
    System.out.println("crash 7");
    this.lat = lat;
    this.lon = lon;
    this.location = location; 
}

public int describeContents() {
    return this.hashCode();
}

// write your object's data to the passed-in Parcel
public void writeToParcel(Parcel dest, int flags) {     
    System.out.println("crash 11");
    dest.writeDouble(lat);
    dest.writeDouble(lon);  
    dest.writeString(location); 
    System.out.println("crash 12");
}   

// this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
public static final Parcelable.Creator<GeoPoints_Class> CREATOR = new Parcelable.Creator<GeoPoints_Class>() {
    public GeoPoints_Class createFromParcel(Parcel in) {
        System.out.println("crash 28");
        return new GeoPoints_Class(in);
    }

    public GeoPoints_Class[] newArray(int size) {
        System.out.println("crash 29");
        return new GeoPoints_Class[size];
    }
};

// example constructor that takes a Parcel and gives you an object populated with it's values
private GeoPoints_Class(Parcel in) {
    System.out.println("crash 13");
     lat = in.readDouble();
     lon = in.readDouble();
     location   = in.readString();
     System.out.println("crash 14");
}

public double getLat() {
    return lat;
}

public void setLat(double lat) {
    this.lat = lat;
}

public double getLon() {
    return lon;
}

public void setLon(double lon) {
    this.lon = lon;
}

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}  

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("GeoPoint Class Details:").append("\n\n");    
   sb.append("latitude: ").append(getLat());
   sb.append("\n");
   sb.append("longitude: ").append(getLon());
   sb.append("\n");
   sb.append("location: ").append(getLocation());
   sb.append("\n");                          

    return sb.toString();
}

}

```

接下来是我使用putExtra放置对象的地方。

```

Intent i = new Intent("com.breckbus.app.ROUTE");
//      i.putParcelableArrayListExtra("route_Classes_temp",  route_Classes);
i.putExtra("route_Classes_temp",route_Classes);
System.out.println("crash 1");

```

然后我得到了对象。

```

Bundle extras = getIntent().getExtras();
Intent d = getIntent();
System.out.println("crash 25");
route_Classes = d.getParcelableArrayListExtra("route_Classes_temp");
System.out.println("crash 2");
//      route_Classes = getIntent().getParcelableArrayListExtra("route_Classes_temp");

```

你会注意到我的所有手动调试语句,因为Eclipse在调试模式下不能与android应用程序一起工作(断点和东西)。 (如果您知道解决方案,那将会非常有帮助。)

接下来是我得到的唯一错误以及我在代码中输入的手动调试语句停止的位置。我不知道它在哪里崩溃了。

以下是第一对错误:

```

10-26 00:03:50.165: I/System.out(323): crash 13
10-26 00:03:50.165: I/System.out(323): crash 14
10-26 00:03:50.165: I/System.out(323): crash 28
10-26 00:03:50.165: I/System.out(323): crash 13
10-26 00:03:50.165: I/System.out(323): crash 14
10-26 00:03:50.185: I/System.out(323): crash 6
10-26 00:03:50.195: D/AndroidRuntime(323): Shutting down VM
10-26 00:03:50.195: W/dalvikvm(323): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-26 00:03:50.225: E/AndroidRuntime(323): FATAL EXCEPTION: main
10-26 00:03:50.225: E/AndroidRuntime(323): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.breckbus.app/com.breckbus.app.route}:    java.lang.RuntimeException: Parcel android.os.Parcel@44edd958: Unmarshalling unknown type code 6553714 at offset 968
10-26 00:03:50.225: E/AndroidRuntime(323):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-26 00:03:50.225: E/AndroidRuntime(323):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-26 00:03:50.225: E/AndroidRuntime(323):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-26 00:03:50.225: E/AndroidRuntime(323):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-26 00:03:50.225: E/AndroidRuntime(323):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 00:03:50.225: E/AndroidRuntime(323):  at android.os.Looper.loop(Looper.java:123)
10-26 00:03:50.225: E/AndroidRuntime(323):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-26 00:03:50.225: E/AndroidRuntime(323):  at java.lang.reflect.Method.invokeNative(Native Method)
10-26 00:03:50.225: E/AndroidRuntime(323):  at java.lang.reflect.Method.invoke(Method.java:521)

```

我希望我提供了足够的信息。尝试使用Intent将Object类型Route_Class的route_class ArrayList从一个活动传递到另一个活动时,请告诉我我做错了什么。这两行:

i.putExtra( “route_Classes_temp”,route_Classes);

route_Classes = d.getParcelableArrayListExtra(“route_Classes_temp”);

感谢您的帮助,非常感谢。

1 个答案:

答案 0 :(得分:0)

您提供的信息确实太多了。

然而,这是在同一个过程(即你自己的应用程序吗?)如果是,那么完全避免这样做。包裹/取消包裹这些物体的速度太慢了。

使用存储数据的单例,只需访问每个活动的单例。