码迷,mamicode.com
首页 > 移动开发 > 详细

Android Parcel初识

时间:2015-09-05 13:52:43      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

先附上一个连接,这里讲的非常详细,感谢此文作者http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html

用Parcelable序列化的对象例子如下,writeToParcel方法里的dest.writeXX顺序要与createFromParcel中parcel.readXX一致,类中一定要有public static final Parcelable.Creator<Phone> CREATOR,否则会抛异常 : 

09-05 01:21:13.696: E/AndroidRuntime(1049): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test2/com.example.test2.ActivityA}: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.example.test2.bean.Phone

 

package com.example.test2.bean;

import android.os.Parcel;
import android.os.Parcelable;

public class Phone implements Parcelable {

public String model;
public float size;
public String manufacture;

public Phone(String manufacture, String model, float size) {
this.manufacture = manufacture;
this.model = model;
this.size = size;
}

public Phone(Parcel in) {
this.manufacture = in.readString();
this.model = in.readString();
this.size = in.readFloat();
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(manufacture);
dest.writeString(model);
dest.writeFloat(size);
}

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

@Override
public Phone createFromParcel(Parcel source) {
return new Phone(source);
}

@Override
public Phone[] newArray(int size) {
return new Phone[size];
}
};

public String toString() {
return manufacture + ", " + model + ", " + size + "寸";
}
}

 

Android Parcel初识

标签:

原文地址:http://www.cnblogs.com/wanjintun/p/4783115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!