导航片段时我的应用崩溃了。 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
}
}
答案 0 :(得分:0)
当您尝试实现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);
}
};