使用传递活动parcelable之间ArrayList对象(Passing ArrayList Ob

2019-10-17 08:34发布

伊利诺伊州首次启动这一关说我都看见了对付这一点,并已经尝试了所有他们的其他职位。

我试着做在其他职位,这是从一个活动传递一个ArrayList中的值到另一个使用意图问了同样的事情。

我觉得我已经正确执行我的课(ES)。 我使用的主类(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");

```

你会发现所有自与调试模式(断点和东西)Android应用程序的Eclipse不工作我的手动调试语句。 (其中,如果你知道解决的是,这将大大帮助)。

接下来是唯一的错误我得到和在那里我的代码停止进入手动调试语句。 我不知道它在哪里崩溃了。

这里是第一对夫妇的错误:

```

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)

```

我希望我提供足够的信息。 试图从一个活动传递对象类型Route_Class的route_class ArrayList中到另一个使用意向时,请告诉我,我做错了。 这两条线:

i.putExtra( “route_Classes_temp”,route_Classes);

route_Classes = d.getParcelableArrayListExtra( “route_Classes_temp”);

谢谢你的帮助,这是非常赞赏。

Answer 1:

这是事实,你提供太多的信息。

但是,这是在同一过程(即自己的应用程序而已?)如果是,则避免这样做完全。 这将是太慢了,包裹/ unparcel这些对象。

使用您存储数据,并从每个活动刚刚访问单身单身。



文章来源: Passing ArrayList Object between activities using parcelable