Passing ArrayList Object between activities using parcelable

此生再无相见时 提交于 2019-12-25 05:25:12

问题


Ill first start this off saying i have seen all the other posts that deal with this and have tried all of them.

Im trying to do the same thing asked in the other posts, which is to pass the values of a ArrayList from one activity to another using Intent.

I feel I have implemented my class(es) correctly. The main class (Route_Class) i am using is below.

```

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

 }

```

Here is my second class (GeoPoints_Class) that is used in Route_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();
}

}

```

Next is where I put the objects using 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");

```

Then where i get the objects.

```

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");

```

You will notice all of my manual debug statements since Eclipse doesnt work with android applications in debug mode (breakpoints and stuff). (Which if you know the solution to that, it would greatly help).

Next is the only error i get and where the manual debug statements i entered in the code stop. I dont know where it is crashing anymore.

Here is the first couple errors:

```

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)

```

I hope i provided enough information. Please tell me what i am doing wrong when trying to pass the route_class ArrayList of Object type Route_Class from one activity to another using Intent. These two lines:

i.putExtra("route_Classes_temp",route_Classes);

route_Classes = d.getParcelableArrayListExtra("route_Classes_temp");

Thank you for your help, it is much appreciated.


回答1:


It is true that you have provided too much information.

However, is this on the same process (i.e. your own app only?) If yes, then avoid doing that entirely. It will be too slow to parcel/unparcel these objects.

Use a singleton where you store your data and just access the singleton from each activity.



来源:https://stackoverflow.com/questions/13079143/passing-arraylist-object-between-activities-using-parcelable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!