Android Parcel First Knowledge

Source: Internet
Author: User

Attach a connection first, here is very detailed, thank the author http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html

Examples of objects serialized with Parcelable are as follows, the Dest.writexx order in the Writetoparcel method is consistent with parcel.readxx in Createfromparcel, and must have public static in the class. Final parcelable.creator<phone> Creator, otherwise it throws an exception:

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 + "inch";
}
}

Android Parcel First Knowledge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.