当我试图通过一个Parcelable传递一个Intent时,我会得到这个错误。基本上,我已经将类实现为Parcelable,但不能将对象传递给另一个活动,因为它在读取对象时会崩溃。我对这个问题尝试了几个答案,但没有一个帮助我解决这个问题。
到目前为止,这是我为WeatherData.java编写的代码(对不起,这个类有点大):
public class WeatherData implements Parcelable {
private final static String ICON_ADDR = "http://openweathermap.org/img/w/";
static class City implements Parcelable {
String name;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
Bundle bundle = new Bundle();
bundle.putString("cityName", name);
dest.writeBundle(bundle);
}
public static final Parcelable.Creator<City> CREATOR = new Creator<City>() {
@Override
public City createFromParcel(Parcel source) {
// read the bundle containing key value pairs from the parcel
Bundle bundle = source.readBundle();
// instantiate the forecast info using values from the bundle
return new City(bundle.getString("cityName"));
}
@Override
public City[] newArray(int size) {
return new City[size];
}
};
private City (String cityName) {
name = cityName;
}
}
static class Weather implements Parcelable {
String description;
String icon;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
Bundle bundle = new Bundle();
bundle.putString("description", description);
bundle.putString("icon", icon);
dest.writeBundle(bundle);
}
public static final Parcelable.Creator<Weather> CREATOR = new Creator<Weather>() {
@Override
public Weather createFromParcel(Parcel source) {
// read the bundle containing key value pairs from the parcel
Bundle bundle = source.readBundle();
// instantiate the forecast info using values from the bundle
return new Weather(bundle.getString("description"),
bundle.getString("icon"));
}
@Override
public Weather[] newArray(int size) {
return new Weather[size];
}
};
private Weather (String description, String icon) {
icon = icon;
description = description;
}
}
static class Temp implements Parcelable {
float day;
float min;
float max;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
Bundle bundle = new Bundle();
bundle.putFloat("day", day);
bundle.putFloat("min", min);
bundle.putFloat("max", max);
dest.writeBundle(bundle);
}
public static final Parcelable.Creator<Temp> CREATOR = new Creator<Temp>() {
@Override
public Temp createFromParcel(Parcel source) {
// read the bundle containing key value pairs from the parcel
Bundle bundle = source.readBundle();
// instantiate the forecast info using values from the bundle
return new Temp(bundle.getFloat("day"),
bundle.getFloat("min"), bundle.getFloat("max"));
}
@Override
public Temp[] newArray(int size) {
return new Temp[size];
}
};
private Temp (float tDay, float tMin, float tMax) {
day = tDay;
min = tMin;
max = tMax;
}
}
static class ForecastInfo implements Parcelable {
Temp temp;
ArrayList<Weather> weather;
String getTemperatureInCelsius() {
float t = temp.day - 273.15f;
return String.format("%.0f" + (char) 0x00B0, t);
};
public String getIconAddress() {
return ICON_ADDR + weather.get(0).icon + ".png";
};
public String getDescription() {
if (weather != null && weather.size() > 0)
return weather.get(0).description;
return null;
};
@Override
public int describeContents() {
return 0;
};
@Override
public void writeToParcel(Parcel dest, int flags) {
Bundle bundle = new Bundle();
bundle.putParcelable("temp", this.temp);
bundle.putParcelableArrayList("weatherList", this.weather);
dest.writeBundle(bundle);
};
public static final Parcelable.Creator<ForecastInfo> CREATOR = new Creator<ForecastInfo>() {
@Override
public ForecastInfo createFromParcel(Parcel source) {
// read the bundle containing key value pairs from the parcel
Bundle bundle = source.readBundle();
// instantiate the forecast info using values from the bundle
return new ForecastInfo((Temp) bundle.get("temp"),
(ArrayList<Weather>) bundle.get("weatherList"));
}
@Override
public ForecastInfo[] newArray(int size) {
return new ForecastInfo[size];
}
};
private ForecastInfo (Temp temp, ArrayList<Weather> weatherList) {
this.temp = temp;
this.weather = weatherList;
};
}
// Relevant data for WeatherData
// The city (which only has a name in it)
City city;
// The array of forecast info extracted from the JSON
ArrayList<ForecastInfo> list;
private WeatherData (City city, ArrayList<ForecastInfo> forecastList) {
city = city;
list = forecastList;
}
// A method that converts temperature from Kelvin degrees to Celsius
String getTemperatureInCelsius(int day) {
return list.get(day).getTemperatureInCelsius();
}
// getIconAddress concatenates the base address and the specific code for
// the icon
public String getIconAddress(int day) {
return list.get(day).getIconAddress();
}
public String getName() {
return city.name;
}
public String getDescription(int day) {
return list.get(day).getDescription();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// create a bundle for the key value pairs
Bundle bundle = new Bundle();
// insert the key value pairs to the bundle
bundle.putString("cityName", this.getName());
bundle.putParcelableArrayList("forecastList", this.list);
// write the key value pairs to the parcel
dest.writeBundle(bundle);
}
public static final Parcelable.Creator<WeatherData> CREATOR = new Creator<WeatherData>() {
@Override
public WeatherData createFromParcel(Parcel source) {
// read the bundle containing key value pairs from the parcel
Bundle bundle = source.readBundle();
return new WeatherData(bundle.getString("cityName"),
new ArrayList<ForecastInfo>());
}
@Override
public WeatherData[] newArray(int size) {
return new WeatherData[size];
}
};
private WeatherData (String name, ArrayList<ForecastInfo> forecastInfos) {
City cityNew = new City(name);
city = cityNew;
forecastInfos.add(new ForecastInfo(
new Temp(1.0f, 1.0f, 1.0f),
new ArrayList<Weather>()));
list = forecastInfos;
}
private WeatherData (String name) {
City cityNew = new City(name);
city = cityNew;
}
}然后,在我的MainActivity中,调用以下函数(对于多次调用setExtrasClassLoader表示抱歉):
public void showDetails(View view) {
Intent intent = new Intent(getApplicationContext(), ShowDetailsActivity.class);
intent.setExtrasClassLoader(WeatherData.class.getClassLoader());
intent.setExtrasClassLoader(WeatherData.ForecastInfo.class.getClassLoader());
intent.setExtrasClassLoader(WeatherData.City.class.getClassLoader());
intent.setExtrasClassLoader(WeatherData.Weather.class.getClassLoader());
intent.setExtrasClassLoader(WeatherData.Temp.class.getClassLoader());
intent.putExtra("bundleObject", weatherData.list);
startActivity(intent);
}之后,我试图通过以下方法获取onCreate方法中新活动中意图的内容:
Intent intent = getIntent();
intent.setExtrasClassLoader(WeatherData.class.getClassLoader());
intent.setExtrasClassLoader(WeatherData.ForecastInfo.class.getClassLoader());
ArrayList<WeatherData.ForecastInfo> list = intent.getParcelableArrayListExtra("bundleObject");如果我在weatherData.getName()中传递weatherData.list而不是在showDetails中传递weatherData.list,它可以工作,但只适用于城市的字符串名称。当我尝试放置一个ArrayList<ForecastInfo>时,它会中断并引发以下错误:
由: android.os.Parcel.readParcelableCreator(Parcel.java:2411)的android.os.BadParcelableException: ClassNotFoundException解编组: com.example.android.climapp.WeatherData$Temp引起在android.os.Parcel.readParcelable(Parcel.java:2337)。
发布于 2016-04-17 00:22:26
解决了!
对于任何努力解决这个问题的人来说,这是因为createFromParcel方法中的新createFromParcel不知道ForecastInfo类。因此,可以这样解决:告诉包通过setClassLoader加载缺失类的数据:
@Override
public WeatherData createFromParcel(Parcel source) {
// read the bundle containing key value pairs from the parcel
Bundle bundle = source.readBundle();
bundle.setClassLoader(ForecastInfo.class.getClassLoader());
// instantiate the weather using values from the bundle
bundle.getParcelable("forecastList"));
return new WeatherData(bundle.getString("cityName"),
new ArrayList<ForecastInfo>());
}https://stackoverflow.com/questions/36670963
复制相似问题