简体   繁体   中英

ClassNotFoundException when unmarshalling Custom Class

I've been encountering a strange ClassNotFoundException when unmarshalling Custom Class. This happens at this.moon = in.readParcelable(Moon.class.getClassLoader());

What is strange is that it happens only on certain devices like Samsung and HTC. Any help will be greatly appreciated. Thank you.

My code snippet is below

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    super.writeToParcel(dest, flags);
    dest.writeParcelable(this.warnings, flags);
    dest.writeTypedList(this.conditions);
    dest.writeParcelable(this.districtForecasts, flags);
    dest.writeParcelable(this.localForecasts, flags);
    dest.writeParcelable(this.partDayForecasts, flags);
    dest.writeParcelable(this.dailyExtremes, flags);
    dest.writeParcelable(this.dailyObservations, flags);
    dest.writeTypedList(this.historicalObservations);
    dest.writeTypedList(this.images);
    dest.writeParcelable(this.forecastRainDates, flags);
    dest.writeParcelable(this.wavesModel,flags);
    dest.writeParcelable(this.tides, flags);
    dest.writeTypedList(this.scripts);
    dest.writeParcelable(this.animator, flags);
    dest.writeParcelable(this.almanac, flags);
    dest.writeParcelable(this.marineForecast, flags);
    dest.writeParcelable(this.moon, flags);
    dest.writeString(this.tag);
    dest.writeParcelable(this.snow,flags);
}

protected LocalWeather(Parcel in) {
    super(in);
    this.warnings = in.readParcelable(Warnings.class.getClassLoader());
    this.conditions = in.createTypedArrayList(Condition.CREATOR);
    this.districtForecasts = in.readParcelable(DistrictForecasts.class.getClassLoader());
    this.localForecasts = in.readParcelable(LocalForecasts.class.getClassLoader());
    this.partDayForecasts = in.readParcelable(PartDayForecasts.class.getClassLoader());
    this.dailyExtremes = in.readParcelable(DailyExtremes.class.getClassLoader());
    this.dailyObservations = in.readParcelable(DailyObservations.class.getClassLoader());
    this.historicalObservations = in.createTypedArrayList(Condition.CREATOR);
    this.images = in.createTypedArrayList(Images.CREATOR);
    this.forecastRainDates = in.readParcelable(RainForecasts.class.getClassLoader());
    this.wavesModel = in.readParcelable(Waves.class.getClassLoader());
    this.tides = in.readParcelable(Tides.class.getClassLoader());
    this.scripts = in.createTypedArrayList(Script.CREATOR);
    this.animator = in.readParcelable(Animator.class.getClassLoader());
    this.almanac = in.readParcelable(Almanac.class.getClassLoader());
    this.marineForecast = in.readParcelable(MarineForecast.class.getClassLoader());
    this.moon =  in.readParcelable(Moon.class.getClassLoader());
    this.tag = in.readString();
    this.snow = in.readParcelable(Snow.class.getClassLoader());
}

My Moon class is below

public class Moon implements Parcelable {
List<MoonPhase> phases = new ArrayList<>();

public List<MoonPhase> getPhases() {
    return phases;
}

public void setPhases(List<MoonPhase> phases) {
    this.phases = phases;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeTypedList(this.phases);
}

public Moon() {
}

protected Moon(Parcel in) {
    this.phases = in.createTypedArrayList(MoonPhase.CREATOR);
}

public static final Parcelable.Creator<Moon> CREATOR = new Parcelable.Creator<Moon>() {
    @Override
    public Moon createFromParcel(Parcel source) {
        return new Moon(source);
    }

    @Override
    public Moon[] newArray(int size) {
        return new Moon[size];
    }
};

public MoonPhase getNextPhase() {
    if (phases != null && phases.size() > 0) {
        return phases.get(0);
    } else {
        return null;
    }
}

}

For List you should use this way :

@Override
public void writeToParcel(Parcel dest, int flags) {
   dest.writeList(this.phases);
}

protected Moon(Parcel in) {
    this.phases = new ArrayList<>();
    in.readList(this.phases , MoonPhase.class.getClassLoader());
}  

The point is using writeList instead of writeTypedList

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM