如何使用嵌套对象创建一个类Parcelable

时间:2013-01-06 02:38:59

标签: android parcelable

我想让A级Parcelable。

public class A {
    public String str;
    public ArrayList<B> list;
}

这是我到目前为止所提出的。但是它崩溃了NullPointerException。问题是这两个陈述:dest.writeList(list);&amp; in.readList(list, this.getClass().getClassLoader());。 我无法弄清楚从这里做什么:(

A类

public class A implements Parcelable {
    public String str;
    public ArrayList<B> list;

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(str);
        dest.writeList(list);
    }

    private A(Parcel in) {
        str = in.readString();
        in.readList(list, this.getClass().getClassLoader());
    }

    public static final Parcelable.Creator<A> CREATOR
            = new Parcelable.Creator<A>() {
        public A createFromParcel(Parcel in) {
            return new A(in);
        }

        public A[] newArray(int size) {
            return new A[size];
        }
    };
}

B类

public class B implements Parcelable {
    public String str;

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(str);
    }

    private B(Parcel in) {
        str = in.readString();
    }

    public static final Parcelable.Creator<B> CREATOR
            = new Parcelable.Creator<B>() {
        public B createFromParcel(Parcel in) {
            return new B(in);
        }

        public B[] newArray(int size) {
            return new B[size];
        }
    };
}

感谢您的时间。

7 个答案:

答案 0 :(得分:51)

我终于找到了输入Google的内容:),并找到了这个Android, How to use readTypedList method correctly in a Parcelable class?

解决方案是使用read-/writeTypedList代替。我还初始化了arraylist以避免任何进一步的NullPointerException。

A类

public class A implements Parcelable {
    public String str;
    public ArrayList<B> list = new ArrayList<B>();

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(str);
        dest.writeTypedList(list);
    }

    private A(Parcel in) {
        str = in.readString();
        in.readTypedList(list, B.CREATOR);
    }

    public static final Parcelable.Creator<A> CREATOR
            = new Parcelable.Creator<A>() {
        public A createFromParcel(Parcel in) {
            return new A(in);
        }

        public A[] newArray(int size) {
            return new A[size];
        }
    };
}

B类

public class B implements Parcelable {
    public String str;

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(str);
    }

    private B(Parcel in) {
        str = in.readString();
    }

    public static final Parcelable.Creator<B> CREATOR
            = new Parcelable.Creator<B>() {
        public B createFromParcel(Parcel in) {
            return new B(in);
        }

        public B[] newArray(int size) {
            return new B[size];
        }
    };
}

答案 1 :(得分:21)

如果您的主Parcelable对象中只有只有一个Parcelable对象,则不会像接受的答案一样列出。然后它将如下所示:

A类

public class A implements Parcelable {
    public String str;
    public B objectB;

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        //The parcelable object has to be the first one
        dest.writeParcelable(objectB, flags);
        dest.writeString(str);
    }

    private A(Parcel in) {
        this.objectB = in.readParcelable(B.class.getClassLoader());
        str = in.readString();
    }

    public static final Parcelable.Creator<A> CREATOR
            = new Parcelable.Creator<A>() {
        public A createFromParcel(Parcel in) {
            return new A(in);
        }

        public A[] newArray(int size) {
            return new A[size];
        }
    };
}

B类

public class B implements Parcelable {
    public String str;

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(str);
    }

    private B(Parcel in) {
        str = in.readString();
    }

    public static final Parcelable.Creator<B> CREATOR
            = new Parcelable.Creator<B>() {
        public B createFromParcel(Parcel in) {
            return new B(in);
        }

        public B[] newArray(int size) {
            return new B[size];
        }
    };
}

重要: 请注意,您编写和阅读Parcelable对象的顺序很重要。查看此answer了解更多详情

答案 2 :(得分:1)

写包裹时

{}

在构造函数中读取它

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeString(name); //if String
    parcel.writeTypedList(assignedEmployees); //if List with custom class, eg. List<AssignedEmployee> assignedEmployees
    parcel.writeParcelable(checkin,i); //if custom class, eg. Checkin checkin;
    }

AssignedEmployee,签入自定义类,它应该实现Parcelable。

答案 3 :(得分:0)

只需按ALT + ENTER并替换Parcelable它将实现所有必要的实现

答案 4 :(得分:0)

您可以从prefs添加Parcelable代码生成器插件,然后您可以通过执行以下操作创建parcelable样板生成器代码: - 右键单击​​模型中的类名 - 选择生成 - 选择Parcelable

presto - 您的模型将使用必要的Parcelable样板代码进行更新。

答案 5 :(得分:0)

我遇到同样的问题是generic version

{
    "name": "xyz",
    "slug": "xyz",
    "supplier": "xyz.limited",
    "attributes": [
        {
            "name": "mass",
            "productConfiguration": "base",
            "description": "",
            "value": "0.24",
            "minimumValue": "",
            "maximumValue": "",
            "measurementUnit": "kg",
            "uuid": "",
            "attributeClassUuid": "",
            "productUuid": ""
        },
        {
            "name": "length",
            "productConfiguration": "base",
            "description": "assumed to be length",
            "value": "58.0",
            "minimumValue": "",
            "maximumValue": "",
            "measurementUnit": "mm",
            "uuid": "",
            "attributeClassUuid": "",
            "productUuid": ""
        },
        {
            "name": "height",
            "productConfiguration": "base",
            "description": "assumed to be height",
            "value": "25.0",
            "minimumValue": "",
            "maximumValue": "",
            "measurementUnit": "mm",
            "uuid": "",
            "attributeClassUuid": "",
            "productUuid": ""
        },
        {
            "name": "width",
            "productConfiguration": "base",
            "description": "assumed to be width",
            "value": "58.0",
            "minimumValue": "",
            "maximumValue": "",
            "measurementUnit": "mm",
            "uuid": "",
            "attributeClassUuid": "",
            "productUuid": ""
        },
        {
            "name": "momentum-storage",
            "productConfiguration": "base",
            "description": "",
            "value": "0.050",
            "minimumValue": "",
            "maximumValue": "",
            "measurementUnit": "N m s",
            "uuid": "",
            "attributeClassUuid": "",
            "productUuid": ""
        },
        {
            "name": "maximum-torque",
            "productConfiguration": "base",
            "description": "",
            "value": "0.007",
            "minimumValue": "",
            "maximumValue": "",
            "measurementUnit": "N m",
            "uuid": "",
            "attributeClassUuid": "",
            "productUuid": ""
        },
        {
            "name": "voltage",
            "productConfiguration": "base",
            "description": "voltage is given in DC current",
            "value": "12.0",
            "minimumValue": "",
            "maximumValue": "",
            "measurementUnit": "V",
            "uuid": "",
            "attributeClassUuid": "",
            "productUuid": ""
        },
        {
            "name": "maximum-power",
            "productConfiguration": "base",
            "description": "",
            "value": "9.0",
            "minimumValue": "",
            "maximumValue": "",
            "measurementUnit": "W",
            "uuid": "",
            "attributeClassUuid": "",
            "productUuid": ""
        },
        {
            "name": "lifetime",
            "productConfiguration": "base",
            "description": "",
            "value": "",
            "minimumValue": "5.0",
            "maximumValue": "",
            "measurementUnit": "yr",
            "uuid": "",
            "attributeClassUuid": "",
            "productUuid": ""
        }
    ]
}

答案 6 :(得分:0)

很不幸,我使用的是来自第三方库的一个类(BarEntry),该类无法打包。我通过遍历Intent的{​​{1}}数组,然后在接收端重建float对象,解决了我的问题。

这不是最佳选择,但是如果有人遇到类似情况(无法创建可打包对象),这可能是一个有用的过程。