Trouble Implementing IParceable In Mono for Android

I am attempting to create a Parcelable class based on the example found in the monodroid samples here

When I attempt to retrieve it from the bundle there is an Exception thrown stating:

Android.OS.BadParcelableException

Parcelable protocol requires a Parcelable.Creator object called CREATOR on class MyClass

    public class Credentials : Object, IParcelable
    {
        [ExportField ("CREATOR")]
        static CredentialsCreator InititalizeCreator()
        {
            return new CredentialsCreator ();
        }

        public String Username { get; set; }
        public String Password { get; set; }

        public Credentials (String username, String password)
        {
            Username = username;
            Password = password;
        }


        public void WriteToParcel (Parcel dest, ParcelableWriteFlags flags)
        {
            dest.WriteString (Username);
            dest.WriteString (Password);
        }

        public int DescribeContents ()
        {
            return 0;
        }

    }

    public class CredentialsCreator: Object, IParcelableCreator
    {
        public Object CreateFromParcel (Parcel source)
        {
            return new Credentials (source.ReadString (), source.ReadString ());
        }

        public Object [] NewArray(int size)
        {
            return new Object[size];
        }


    }

I noticed in the example there are three lines of code that I don't understand.

var p = Parcel.Obtain ();
p.WriteBundle (b);
p.SetDataPosition (0);

Answers

  • JonathanPryorJonathanPryor USXamarin Team Xamurai

    When I attempt to retrieve it from the bundle there is an Exception thrown stating:

    Your code works for me. Perhaps you need to remove your obj and bin directories and rebuild?

    I noticed in the example there are three lines of code that I don't understand.

    >

    var p = Parcel.Obtain ();
    p.WriteBundle (b);
    p.SetDataPosition (0);
    

    After p.SetDataPosition(0), p.ReadBundle() is invoked. The p.SetDataPosition(0) is required so that p will attempt to read a Bundle located at the beginning of p. Without p.SetDataPosition(0), p.ReadBundle() would attempt to read a Bundle from the current position of p, which would be after the Bundle just written.

    Perhaps it makes more sense if you replace Parcel with Stream, and p.SetDataPosition(0) with p.Position=0. It's just a way to make sure that the data just written is read.

  • Could someone help me please in this topic?
    I'm struggling with no end in this. I get some error as Brandon Grant wrote.

    I took his 'Credentials' class and sent data by :

    Credentials c = new Credentials ("user", "pass"); var act = new Intent(this, typeof(TestActivity)); act.PutExtra("data", c); SendBroadcast (act); StartActivity(act);

    Receiving data I tried by:

    Credentials data = (Credentials)Intent.GetParcelableExtra("data");

  • BrandonGrantBrandonGrant USMember ✭✭

    I was able to get it working just had to add public to this declaration

    static CredentialsCreator InititalizeCreator() { return new CredentialsCreator (); }

Sign In or Register to comment.