您好我的应用程序有问题。在我将它们改为parcelable之后,我的类首先是可序列化的,并且出现了问题。问题是,当我浏览应用程序内部的碎片时,有时它会崩溃。请希望在这里我可以得到一些帮助 这是我的2个类和logcat消息。谢谢你的关注
City.java
public class City implements Parcelable {
private String code;
private String name;
private List<CityWeather> weathers ;
public City() {
super();
}
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);
}
public City(Parcel in) {
readFromParcel(in);
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(code);
dest.writeString(name);
dest.writeTypedList(weathers);
}
private void readFromParcel(Parcel in) {
code = in.readString();
name = in.readString();
in.readTypedList(weathers, CityWeather.CREATOR);
}
public static final Parcelable.Creator<City> CREATOR = new Parcelable.Creator<City>() {
public City createFromParcel(Parcel in) {
return new City(in);
}
public City[] newArray(int size) {
return new City[size];
}
};
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
}
CityWeather.java
public class CityWeather implements Parcelable {
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;
}
protected CityWeather(Parcel in) {
date = in.readString();
weatherDescription = in.readString();
weatherCode = in.readInt();
currentTemperature = in.readString();
minTemperature = in.readString();
maxTemperature = in.readString();
humadity = in.readString();
wind = in.readString();
day = in.readString();
temperatureUnit = in.readString();
}
public CityWeather() {
// TODO Auto-generated constructor stub
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(date);
dest.writeString(weatherDescription);
dest.writeInt(weatherCode);
dest.writeString(currentTemperature);
dest.writeString(minTemperature);
dest.writeString(maxTemperature);
dest.writeString(humadity);
dest.writeString(wind);
dest.writeString(day);
dest.writeString(temperatureUnit);
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<CityWeather> CREATOR = new Parcelable.Creator<CityWeather>() {
@Override
public CityWeather createFromParcel(Parcel in) {
return new CityWeather(in);
}
@Override
public CityWeather[] newArray(int size) {
return new CityWeather[size];
}
};
}
和logcat消息
02-16 17:30:23.014: E/AndroidRuntime(15818): FATAL EXCEPTION: main
02-16 17:30:23.014: E/AndroidRuntime(15818): java.lang.RuntimeException: Unable to start activity ComponentInfo{ge.mobility.weather/ge.mobility.weather.activity.MainActivity}: java.lang.NullPointerException
02-16 17:30:23.014: E/AndroidRuntime(15818): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
02-16 17:30:23.014: E/AndroidRuntime(15818): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
02-16 17:30:23.014: E/AndroidRuntime(15818): at android.app.ActivityThread.access$600(ActivityThread.java:151)
02-16 17:30:23.014: E/AndroidRuntime(15818): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
02-16 17:30:23.014: E/AndroidRuntime(15818): at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 17:30:23.014: E/AndroidRuntime(15818): at android.os.Looper.loop(Looper.java:155) 02-16 17:30:23.014: E/AndroidRuntime(15818): at android.app.ActivityThread.main(ActivityThread.java:5520)
02-16 17:30:23.014: E/AndroidRuntime(15818): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 17:30:23.014: E/AndroidRuntime(15818): at java.lang.reflect.Method.invoke(Method.java:511)
02-16 17:30:23.014: E/AndroidRuntime(15818): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
02-16 17:30:23.014: E/AndroidRuntime(15818): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
02-16 17:30:23.014: E/AndroidRuntime(15818): at dalvik.system.NativeStart.main(Native Method)
02-16 17:30:23.014: E/AndroidRuntime(15818): Caused by: java.lang.NullPointerException
02-16 17:30:23.014: E/AndroidRuntime(15818): at android.os.Parcel.readTypedList(Parcel.java:1754)
02-16 17:30:23.014: E/AndroidRuntime(15818): at ge.mobility.weather.entity.City.readFromParcel(City.java:62)
02-16 17:30:23.014: E/AndroidRuntime(15818): at ge.mobility.weather.entity.City.(City.java:49)
02-16 17:30:23.014: E/AndroidRuntime(15818): at ge.mobility.weather.entity.City$1.createFromParcel(City.java:67)
02-16 17:30:23.014: E/AndroidRuntime(15818): at ge.mobility.weather.entity.City$1.createFromParcel(City.java:1)
02-16 17:30:23.014: E/AndroidRuntime(15818): at android.os.Parcel.readParcelable(Parcel.java:2108)
答案 0 :(得分:0)
在没有初始化天气列表的情况下调用createFromParcel时,可能会发生NPE。
尝试替换
private List<CityWeather> weathers;
带
private List<CityWeather> weathers = new ArrayList<CityWeather>();
答案 1 :(得分:0)
我猜天气是空的。你应该确保在解组你的对象之前实例化列表。
private void readFromParcel(Parcel in) {
code = in.readString();
name = in.readString();
in.readTypedList(weathers, CityWeather.CREATOR); //HERE IT FAILS
}
试试这个
private void readFromParcel(Parcel in) {
code = in.readString();
name = in.readString();
if(weathers == null){
weathers = new ArrayList<CityWeather>();
}
in.readTypedList(weathers, CityWeather.CREATOR);
}
答案 2 :(得分:-1)
替代方法是使用Gson将对象转换为JSONObject。
初始活动
Intent activity = new Intent(MyActivity.this,NextActivity.class);
activity.putExtra("myObject", new Gson().toJson(myobject);
startActivity(activity);
下一个活动
Sting jsonMyObject;
Bundle extras = getIntent().getExtras();
if (extras != null) {
jsonMyObject = extras.getString("myObject");
}
MyObject myObject = new Gson().fromJson(jsonMyObject, MyObject.class);