Unmarshalling errors in Android app with custom parcelable classes

后端 未结 3 1512
有刺的猬
有刺的猬 2020-12-04 22:52

For my Android application, I get several unmarshalling errors although I think I\'ve done everything that is needed to properly save and load objects via Parcelable

3条回答
  •  时光取名叫无心
    2020-12-04 23:16

    By the look of it, the createFromParcel and newArray should be overridden like this:

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        @Override
        public MyObject createFromParcel(Parcel in) {
            MyObject myObj = new MyObject();
            myObj.intArray = in.readIntArray(...);
            myObj.intValue = in.readInt(...);
            // ....
            // IN THE SAME ORDER THAT IS WRITTEN OUT AS PER writeToParcel!
            //
            return myObj;
        }
        @Override
        public MyObject[] newArray(int size) {
            return new MyObject[size];
        }
    };
    

    Edit:

    I forgot to mention that for the above to work, there should have been an empty constructor!

    public MyObject(){}
    

提交回复
热议问题