Android:将Parcelable对象传递给另一个Activity时出错

时间:2015-11-04 09:18:28

标签: java android arraylist parcelable

我正在尝试将一个对象从我的MainActivity传递给另一个。它有一个ArrayList类型的属性,它被更改,我不知道为什么。在MainActivity中,我使用对象的toStringSystem.out.println()来查看他拥有的内容,我可以看到:

Incidente{name='Farola fundida', type='sin luz', street='falsa', streetNumber=123, city='Bilbao', contacts=[alguien1, alguien2]}

然而,当我在另一项活动上收到它时,我又制作了另一个System.out.println(),我得到了这个:

Incidente{name='Farola fundida', type='sin luz', street='falsa', streetNumber=123, city='Bilbao', contacts=[, null]}

你能告诉我我做错了吗?

我的班级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;
    }
}

这是我发送对象的方式:

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

这就是我收到它的方式:

incidente=getIntent().getExtras().getParcelable("objeto");

2 个答案:

答案 0 :(得分:3)

来自createStringArrayList的文档:

  

从中读取并返回一个包含String对象的新ArrayList   在当前使用writeStringList(List)编写的parcel   dataPosition()。

因此,根据您应该使用writeStringList(List)而不是writeList

的文档

答案 1 :(得分:0)

使用parcelabler生成

# Virtual hosts
Include conf/extra/httpd-vhosts.conf