0

我到处看了看,我想我错过了一些东西......我试图将我的 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");
4

1 回答 1

4

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

public static final Parcelable.Creator<EcgResultsHolder> CREATOR 
      = new Parcelable.Creator<EcgResultsHolder>() {
  ...
}
于 2013-11-03T15:58:50.460 回答