[英]Android: Error at passing a Parcelable object to another Activity
I'm trying to pass one object from my MainActivity to another. 我正在尝试将一个对象从MainActivity传递给另一个对象。 It has an atribute of the type ArrayList which is changed and I don't know why. 它具有ArrayList类型的属性,该属性已更改,我不知道为什么。 In the MainActivity I use object's toString
and System.out.println()
to see what he has and I can see this: 在MainActivity中,我使用对象的toString
和System.out.println()
来查看其拥有的内容,并且可以看到以下内容:
Incidente{name='Farola fundida', type='sin luz', street='falsa', streetNumber=123, city='Bilbao', contacts=[alguien1, alguien2]}
However, when I receive it on the other activity I make another System.out.println()
and I get this: 但是,当我在其他活动上收到它时,我创建了另一个System.out.println()
并且得到了以下信息:
Incidente{name='Farola fundida', type='sin luz', street='falsa', streetNumber=123, city='Bilbao', contacts=[, null]}
Can you tell me what I'm doing wrong? 你能告诉我我做错了吗?
My class Incidente
code is this: 我的班级Incidente
代码是这样的:
public class Incidente implements Parcelable{
String name;
String type;
String street;
int streetNumber;
String city;
ArrayList<String> contacts;
public Incidente(String name, String type, String street, int streetNumber, String city, ArrayList<String> contacts) {
this.name = name;
this.type = type;
this.street = street;
this.streetNumber = streetNumber;
this.city = city;
this.contacts = contacts;
}
protected Incidente(Parcel in) {
name = in.readString();
type = in.readString();
street = in.readString();
streetNumber = in.readInt();
city = in.readString();
contacts = in.createStringArrayList();
}
public static final Creator<Incidente> CREATOR = new Creator<Incidente>() {
@Override
public Incidente createFromParcel(Parcel in) {
return new Incidente(in);
}
@Override
public Incidente[] newArray(int size) {
return new Incidente[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(type);
dest.writeString(street);
dest.writeInt(streetNumber);
dest.writeString(city);
dest.writeList(contacts);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public int getStreetNumber() {
return streetNumber;
}
public void setStreetNumber(int streetNumber) {
this.streetNumber = streetNumber;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public ArrayList<String> getContacts() {
return contacts;
}
public void setContacts(ArrayList<String> contacts) {
this.contacts = contacts;
}
@Override
public String toString() {
return "Incidente{" +
"name='" + name + '\'' +
", type='" + type + '\'' +
", street='" + street + '\'' +
", streetNumber=" + streetNumber +
", city='" + city + '\'' +
", contacts=" + contacts +
'}';
}
public boolean equals (Incidente obj2)
{
if(this.getName().equals(obj2.getName()))
{
if(this.getType().equals(obj2.getType()))
{
if(this.getStreet().equals(obj2.getStreet()))
{
if(this.getStreetNumber()== obj2.getStreetNumber())
{
if(this.getCity().equals(obj2.getStreetNumber()))
{
if(this.getContacts().equals(obj2.getContacts()))
{
return true;
}
}
}
}
}
}
return false;
}
}
This is how I send the object: 这是我发送对象的方式:
ArrayList<String> arrContactos=new ArrayList<String>();
arrContactos.add("alguien1");
arrContactos.add("alguien2");
Incidente inci = new Incidente("Farola fundida", "sin luz", "falsa", 123, "Bilbao", arrContactos);
Intent i = new Intent(this, DetalleIncidenteActivity.class);
i.putExtra("objeto",inci);
And this is how I receive it: 这是我收到的方式:
incidente=getIntent().getExtras().getParcelable("objeto");
From the documentation of createStringArrayList : 从createStringArrayList的文档中:
Read and return a new ArrayList containing String objects from the parcel that was written with writeStringList(List) at the current dataPosition(). 从当前dataPosition()处用writeStringList(List)编写的包裹中读取并返回一个新的ArrayList,其中包含String对象。
So, accordingly to the documentation you should be using writeStringList(List)
instead of writeList
因此,根据文档,您应该使用writeStringList(List)
而不是writeList
Generated using parcelabler 使用parcelabler生成
public class Incidente implements Parcelable, Parcelable {
String name;
String type;
String street;
int streetNumber;
String city;
ArrayList<String> contacts;
protected Incidente(Parcel in) {
name = in.readString();
type = in.readString();
street = in.readString();
streetNumber = in.readInt();
city = in.readString();
if (in.readByte() == 0x01) {
contacts = new ArrayList<String>();
in.readList(contacts, String.class.getClassLoader());
} else {
contacts = null;
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(type);
dest.writeString(street);
dest.writeInt(streetNumber);
dest.writeString(city);
if (contacts == null) {
dest.writeByte((byte) (0x00));
} else {
dest.writeByte((byte) (0x01));
dest.writeList(contacts);
}
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<Incidente> CREATOR = new Parcelable.Creator<Incidente>() {
@Override
public Incidente createFromParcel(Parcel in) {
return new Incidente(in);
}
@Override
public Incidente[] newArray(int size) {
return new Incidente[size];
}
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.