无法在活动之间传递Parcelable数据

时间:2013-11-03 15:46:26

标签: android

我到处寻找,我想我错过了一些东西......我试图将我的ArrayList与我的对象从活动传递到活动,并获得“无法启动活动...类型为EcgResultsHolder的空指针异常预期接收器”。 ..和应用程序崩溃。

类型:

    public class EcgDetails
{
    private final String m_EcgProperties;
    private final String m_Category;

    public EcgDetails(String i_EcgProps, String i_Category)
    {
        m_EcgProperties = i_EcgProps;
        m_Category = i_Category;
    }

    public String getEcgParams()
    {
        return m_EcgProperties;
    }

    public String getCategory()
    {
        return m_Category;
    }
}

public class EcgResultsHolder extends ArrayList<EcgDetails> implements Parcelable
{
    private static final long serialVersionUID = 663585476779879096L;

    public EcgResultsHolder( )
    {

    }

    public EcgResultsHolder(Parcel in){
        readFromParcel(in);
    }

    @SuppressWarnings("unchecked")
    public final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        @Override
        public EcgResultsHolder createFromParcel(Parcel in) {
            return new EcgResultsHolder(in);
        }

        @Override
        public Object[] newArray(int arg0) {
            return null;
        }

    };

    private void readFromParcel(Parcel in) {
        this.clear();

        //First we have to read the list size
        int size = in.readInt();

        //Reading remember that we wrote first the Name and later the Phone Number.
        //Order is fundamental

        for (int i = 0; i < size; i++) 
        {
            EcgDetails c = new EcgDetails(in.readString(), in.readString());
            this.add(c);
        }

    }




    @Override
    public void writeToParcel(Parcel dest, int flags) 
    {
        int size = this.size();
        //We have to write the list size, we need him recreating the list
        dest.writeInt(size);
        //We decided arbitrarily to write first the Name and later the Phone Number.
        for (int i = 0; i < size; i++) {
            EcgDetails c = this.get(i);
            dest.writeString(c.getEcgParams());
            dest.writeString(c.getCategory());
        }
    }

    @Override
    public int describeContents()
    {
        return 0;
    }   

}

这就是我说的:

Bundle bundle = new Bundle();  
        bundle.putParcelable("EcgList", m_EcgListFromServer);
        intent.putExtras(bundle);

这是我在被叫活动上阅读的方式:

EcgResultsHolder m_EcgList = getIntent().getExtras().getParcelable("EcgList");

1 个答案:

答案 0 :(得分:4)

静态限定符添加到CREATOR字段。它必须是静态的。

public static final Parcelable.Creator<EcgResultsHolder> CREATOR 
      = new Parcelable.Creator<EcgResultsHolder>() {
  ...
}