0

my app crashes when navigating through the fragments. the logcat error tells BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class ge.mobility.weather.entity.City. so I need my classes to implement parcelable and then i want to pass a City object through the fragment.

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

and the secnd class

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

    }
}

1 Answer 1

0

When you try implement Parcelable then you must have to create an obeject named CREATOR inside your class which implementing the Parcelable interface as example for your City class...

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.