Reputation: 29
When I try to send an object to another Activity
, it shows this error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.movieapp/com.example.android.movieapp.DetailActivity}: android.os.BadParcelableException: Parcelable protocol requires the CREATOR object to be static on class com.example.android.movieapp.Movie
This is my code:
public class Movie implements Parcelable{
String title;
String image;
public Movie (String title, String image){
this.title = title;
this.image = image;
}
public Movie(JSONObject movie) throws JSONException {
this.title = movie.getString("original_title");
this.image = movie.getString("poster_path");
}
private Movie (Parcel in){
title = in.readString();
image = in.readString();
}
public final Parcelable.Creator<Movie> CREATOR = new Parcelable.Creator<Movie>(){
@Override
public Movie createFromParcel(Parcel parcel) {
return new Movie(parcel);
}
@Override
public Movie[] newArray(int i) {
return new Movie[i];
}
};
@Override
public String toString() {
return title + "--" + image;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(title);
dest.writeString(image);
}
public String getImage() {
return image;
}
public String getTitle() {
return title;
}
}
Main Class:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Movie movie = movieAdapter.getItem(position);
Intent intent = new Intent(getApplication(), DetailActivity.class);
intent.putExtra("send", movie);
startActivity(intent);
}
});
}
and DetailClass:
Movie movie;
Bundle extras = getIntent().getExtras();
movie = extras.getParcelable("send");
title.setText(movie.title);
Upvotes: 1
Views: 1778
Reputation: 19969
The exception states:
...the CREATOR object to be static on class com.example.android.movieapp.Movie
Also, from the Parcelable documentation:
Classes implementing the Parcelable interface must also have a non-null static field called CREATOR of a type that implements the Parcelable.Creator interface.
CREATOR
has to be static:
public static final Parcelable.Creator<Movie> CREATOR =
Upvotes: 3