解组自定义类

时间:2017-06-11 03:01:24

标签: java android classnotfoundexception

在解组Custom Class时,我遇到了一个奇怪的ClassNotFoundException。这发生在     this.moon = in.readParcelable(Moon.class.getClassLoader());

奇怪的是,它只发生在三星和HTC等某些设备上。任何帮助将不胜感激。谢谢。

我的代码段位于

之下
@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());
}

我的月亮课程在

之下
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;
    }
}

}

1 个答案:

答案 0 :(得分:0)

对于List,您应该使用这种方式:

@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());
}  
  

重点是使用writeList而不是writeTypedList