Problem unmarshalling parcelables

后端 未结 10 1949
旧时难觅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:19

    I got this exception because I was missing a constructor. The same must be done for all classes that implement Parcelable:

    // add new constructor
    @RequiresApi(Build.VERSION_CODES.N)
    private LocationType(Parcel dest, ClassLoader loader) {
        super(dest, loader);
        locid = dest.readInt();
        desc  = dest.readString();
        dir   = dest.readString();
        lat   = dest.readDouble();
        lng   = dest.readDouble();
    }
    
    public static final Creator CREATOR = new ClassLoaderCreator() {
    
        // add createFromParcel method with ClassLoader
        @Override
        public LayoverType createFromParcel(Parcel in, ClassLoader loader)
        {
            return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? new LayoverType(in, loader) : new LayoverType(in);
        }
    
        public LayoverType createFromParcel(Parcel in) {
            // call other createFromParcel method.    
            return createFromParcel(in, null);
        }
    
        public LayoverType[] newArray(int size) {
            return new LayoverType[size];
        }
    };
    

提交回复
热议问题