简体   繁体   中英

Implementing Parcelable for Date Object - NullPointerException

I am trying to pull the release date value from TheMovieDatabase API. One of the items it returns is a Date value.

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeInt(vote_count);
    parcel.writeInt(id);
    parcel.writeByte((byte) (video ? 1:0));
    parcel.writeString(title);
    parcel.writeFloat(popularity);
    parcel.writeString(poster_path);
    parcel.writeString(original_language);
    parcel.writeString(original_title);
    parcel.writeStringList(genre_ids);
    parcel.writeString(backdrop_path);
    parcel.writeByte((byte) (adult ? 1:0));
    parcel.writeString(overview);
    parcel.writeLong(release_date.getTime());


}

IndividualMovieActivity.java:

public class IndividualMovieActivity extends AppCompatActivity {

private TextView mMovieTitle;
private ImageView mMoviePoster;
private TextView mMovieReleaseDate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_individual_movie);

    Intent intent = getIntent();
    Movie movie = intent.getParcelableExtra("Movie");
    String title = movie.getOriginal_title();
    Log.v("POSTER_PATH", movie.getPoster_path());
    Log.v("RELEASE", movie.getRelease_date().toString());

    mMovieTitle = findViewById(R.id.movie_name);
    mMoviePoster = findViewById(R.id.movie_details_movie_poster_image);
    mMovieReleaseDate = findViewById(R.id.movie_details_release_date);

    mMovieTitle.setText(title);
    Picasso.with(getApplicationContext())
            .load("http://image.tmdb.org/t/p/w185" + movie.getPoster_path())
            .into(mMoviePoster);

    mMovieReleaseDate.setText((CharSequence) movie.getRelease_date());

   }
}

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.util.Date.toString()' on a null object reference at popularmovies.troychuinard.com.popularmovies.IndividualMovieActivity.onCreate(IndividualMovieActivity.java:29)

Full Source Code: https://github.com/troy21688/PopularMovies

In your project's linked source code, these are the parcelable "read" and "write" methods:

 protected Movie(Parcel in) { vote_count = in.readInt(); id = in.readInt(); video = in.readByte() != 0; title = in.readString(); popularity = in.readFloat(); poster_path = in.readString(); original_language = in.readString(); original_title = in.readString(); genre_ids = in.createStringArrayList(); backdrop_path = in.readString(); adult = in.readByte() != 0; overview = in.readString(); } @Override public void writeToParcel(Parcel parcel, int i) { parcel.writeInt(vote_count); parcel.writeInt(id); parcel.writeByte((byte) (video ? 1:0)); parcel.writeString(title); parcel.writeFloat(popularity); parcel.writeString(poster_path); parcel.writeString(original_language); parcel.writeString(original_title); parcel.writeStringList(genre_ids); parcel.writeString(backdrop_path); parcel.writeByte((byte) (adult ? 1:0)); parcel.writeString(overview); parcel.writeLong(release_date.getTime()); } 

If you look carefully, you'll see that the Parcel constructor doesn't have a "read" operation for release_date . You probably need to add something like this:

release_date = new Date(in.readLong());

查看源代码 ,它似乎不像Movie(Parcel in)构造函数初始化this.release_date ,因此在尝试访问它时为null。

I have created a similar app called Cinemate using "TheMovieDatabase API" as a project, part of Udacity's Android Nanodegree program.

It appears to me, you may have a type problem. Try retrieving the date from the endpoint as a String and modify your Movie class accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM