为Date对象实现Parcelable - NullPointerException

时间:2018-06-14 01:46:57

标签: java android parcelable

我正在尝试从TheMovieDatabase API中提取发布日期值。它返回的一个项目是Date值。

@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());

   }
}
  

引起:java.lang.NullPointerException:尝试调用虚拟   null对象上的方法'java.lang.String java.util.Date.toString()'   参考               at popularmovies.troychuinard.com.popularmovies.IndividualMovieActivity.onCreate(IndividualMovieActivity.java:29)

完整源代码: https://github.com/troy21688/PopularMovies

3 个答案:

答案 0 :(得分:2)

在您项目的链接源代码中,这些是可以包含的" read"和"写"方法:

<li>

如果仔细观察,您会发现包裹构造函数没有&#34;读取&#34; 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()); } 的操作。您可能需要添加以下内容:

release_date

答案 1 :(得分:1)

查看source codeMovie(Parcel in)构造函数似乎不会初始化this.release_date,因此当您尝试访问它时它为null。

答案 2 :(得分:1)

我使用&#34; TheMovieDatabase API&#34;创建了一个名为Cinemate的类似应用。作为一个项目,是Udacity的Android Nanodegree计划的一部分。

在我看来,您可能遇到类型问题。尝试以String的形式从端点检索日期,并相应地修改您的Movie课程。