【问题标题】:making impementation of parcelable classes实现parcelable类
【发布时间】:2014-02-15 20:37:17
【问题描述】:

我的应用在浏览片段时崩溃。 logcat 错误告诉 BadParcelableException:Parcelable 协议需要在类 ge.mobility.weather.entity.City 上名为 CREATOR 的 Parcelable.Creator 对象。所以我需要我的类来实现 parcelable,然后我想通过片段传递一个 City 对象。

import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.List;

public class City implements Parcelable {
    private String code;
    private String name;

    private List<CityWeather> weathers ;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public List<CityWeather> getWeathers() {
        if(weathers == null) {
            weathers = new ArrayList<CityWeather>();
        }
        return weathers;
    }
    public void addCityWeather(CityWeather w) {
        getWeathers().add(w);
    }

    public void addCityWeathers(List<CityWeather> w) {
        getWeathers().addAll(w);
    }
    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
}

第二类

import android.os.Parcel;
import android.os.Parcelable;

public class CityWeather  {

    private String date;
    private String weatherDescription;
    private int weatherCode;
    private String currentTemperature;
    private String minTemperature;
    private String maxTemperature;
    private String humadity;
    private String wind;
    private String day;
    private String temperatureUnit;

    public String getTemperatureUnit(){
        return temperatureUnit;
    }
    public void setTemperatureUnit(String temperatureUnit){
        this.temperatureUnit = temperatureUnit;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public String getWeatherDescription() {
        return weatherDescription;
    }
    public void setWeatherDescription(String weatherDescription) {
        this.weatherDescription = weatherDescription;
    }
    public int getWeatherCode() {
        return weatherCode;
    }
    public void setWeatherCode(int weatherCode) {
        this.weatherCode = weatherCode;
    }
    public String getCurrentTemperature() {
        return currentTemperature;
    }
    public void setCurrentTemperature(String currentTemperature) {
        this.currentTemperature = currentTemperature;
    }
    public String getMinTemperature() {
        return minTemperature;
    }
    public void setMinTemperature(String minTemperature) {
        this.minTemperature = minTemperature;
    }
    public String getMaxTemperature() {
        return maxTemperature;
    }
    public void setMaxTemperature(String maxTemperature) {
        this.maxTemperature = maxTemperature;
    }
    public String getHumadity() {
        return humadity;
    }
    public void setHumadity(String humadity) {
        this.humadity = humadity;
    }
    public String getWind() {
        return wind;
    }
    public void setWind(String wind) {
        this.wind = wind;
    }
    public String getDay() {
        return day;
    }
    public void setDay(String day) {
        this.day = day;
    }
    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub

    }
}

【问题讨论】:

    标签: java android


    【解决方案1】:

    当您尝试实现 Parcelable 时,您必须在您的类中创建一个名为 CREATOR 的对象,该对象实现 Parcelable 接口作为您的 City 类的示例...

    public static final Parcelable.Creator<City> CREATOR = new Creator<City>() {
    
        @Override
        public City[] newArray(int size) {
            return new City[size];
        }
    
        @Override
        public City createFromParcel(Parcel in) {
            return new City(in);
        }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-25
      • 1970-01-01
      相关资源
      最近更新 更多