Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Lists and Adapters (2015) Using Parcelable Data Retrieving Parcelable Data

Tsvetan Rankov
Tsvetan Rankov
1,396 Points

FATAL EXCEPTION: main ... android.os.BadParcelableException

The app is running without errors but when I press 7 Days button it stops: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tsvetan.stormy/com.example.tsvetan.stormy.ui.DailyForecastActivity}: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.example.tsvetan.stormy.weather.Day at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)....

I tried with the solution from this question but the issue remains the same https://teamtreehouse.com/forum/fatal-exception-main-2

Could anyone help?

5 Answers

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

Hi Tsvetan,

In the getView method of your adapter, when you are setting your holder.dayLabel you have a space between R.id. and dayNameLabel. Maybe that will fix your error?

If not let, me know.

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

Hi Tsvetan,

Could you post the code to your Day class? It looks like the error is occurring there.

Tsvetan Rankov
Tsvetan Rankov
1,396 Points
public class Day implements Parcelable {
    private long Time;
    private String Summary;
    private double TemperatureMax;
    private String Icon;
    private String TimeZone;

    public Day() { }

    public long getTime() {
        return Time;
    }

    public void setTime(long time) {
        Time = time;
    }

    public String getSummary() {
        return Summary;
    }

    public void setSummary(String summary) {
        Summary = summary;
    }

    public int getTemperatureMax() {
        return (int)Math.round(TemperatureMax);
    }

    public void setTemperatureMax(double temperatureMax) {
        TemperatureMax = temperatureMax;
    }

    public String getIcon() {
        return Icon;
    }

    public void setIcon(String icon) {
        Icon = icon;
    }

    public String getTimeZone() {
        return TimeZone;
    }

    public void setTimeZone(String timeZone) {
        TimeZone = timeZone;
    }

    public int getIconId() {
        return Forecast.getIconId(Icon);
    }

    public String  getDayOfTheWeek() {
        SimpleDateFormat formatter = new SimpleDateFormat("EEEE");
        formatter.setTimeZone(java.util.TimeZone.getTimeZone(TimeZone));
        Date dateTime = new Date(Time * 1000);
        return formatter.format(dateTime);
    }



    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(this.Time);
        dest.writeString(this.Summary);
        dest.writeDouble(this.TemperatureMax);
        dest.writeString(this.Icon);
        dest.writeString(this.TimeZone);

    }

    private Day(Parcel in) {
        Time = in.readLong();
        Summary = in.readString();
        TemperatureMax  = in.readDouble();
        Icon = in.readString();
        TimeZone = in.readString();
    }

    public static final Creator<Day> CREATOR = new Parcelable.Creator<Day>() {
        @Override
        public Day createFromParcel(Parcel source) {
            return new Day(source);
        }

        @Override
        public Day[] newArray(int size) {
            return new Day[size];
        }
    };
}
Jon Kussmann
Jon Kussmann
Courses Plus Student 7,254 Points

This class does look okay to me. Could you post your DayAdapter class?

Tsvetan Rankov
Tsvetan Rankov
1,396 Points
public class DayAdapter extends BaseAdapter {
    public static final String TAG = DayAdapter.class.getSimpleName();
    private Context context;
    private Day[] days;
    public DayAdapter(Context newContext, Day[] newDays) {
        context = newContext;
        days = newDays;
    }
    @Override
    public int getCount() {
        return days.length;
    }

    @Override
    public Object getItem(int position) {
        return days[position];
    }

    @Override   
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if(convertView == null) { //brand new
            convertView = LayoutInflater.from(context).inflate(R.layout.daily_list_item, null);
            holder = new ViewHolder();
            holder.iconImageView = (ImageView) convertView.findViewById(R.id.iconImageView);
            holder.temperatureLabel = (TextView) convertView.findViewById(R.id.temperatureLabel);
            holder.dayLabel = (TextView) convertView.findViewById(R.id. dayNameLabel);
        }
        else {
            holder = (ViewHolder) convertView.getTag();

        }
        Day day = days[position];

        holder.iconImageView.setImageResource(day.getIconId());
        //holder.temperatureLabel.setText(day.getTemperatureMax() + "");
        try { holder.temperatureLabel.setText(day.getTemperatureMax() + ""); }
        catch (Exception e){ Log.e(TAG, "Fatal Exception", e); }
        holder.dayLabel.setText(day.getDayOfTheWeek());

        return convertView;
    }

    private static class ViewHolder {
        ImageView iconImageView;
        TextView temperatureLabel;
        TextView dayLabel;
    }
}
Tsvetan Rankov
Tsvetan Rankov
1,396 Points

Hi Jon, After correcting this simple mistake I had NullPointerException and I read the solutions from the other questions and I debugged it. Now it works fine. Thank you for your help.