Problem unmarshalling parcelables

后端 未结 10 1925
旧时难觅i
旧时难觅i 2020-11-27 15:45

I\'ve got a few classes that implement Parcelable and some of these classes contain each other as properties. I\'m marshalling the classes into a Parcel to pass them between

10条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 16:06

    Just adding my 2 cents here, because I lost more than half a day scratching my head on this. You might get this error if you don't put the writes methods and reads methods in the exact same order. For instance the following would be wrong:

     @Override
        // Order: locid -> desc -> lat -> dir -> lng
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt   (locid);
            dest.writeString(desc);
            dest.writeDouble(lat);
            dest.writeString(dir);
            dest.writeDouble(lng);
        }
        // Order: locid -> desc -> dir -> lat -> lng
        private LocationType(Parcel dest) {
            locid = dest.readInt();
            desc  = dest.readString();
            dir   = dest.readString();
            lat   = dest.readDouble();
            lng   = dest.readDouble();
        }
    

    By the way the author did this correctly but it might help someone one day.

提交回复
热议问题