gpt4 book ai didi

java - 为日期对象实现 Parcelable - NullPointerException

转载 作者:行者123 更新时间:2023-12-01 17:50:24 27 4
gpt4 key购买 nike

我正在尝试从 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());

}
}

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)

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

最佳答案

在项目的链接源代码中,这些是可分割的“读取”和“写入”方法:

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

如果仔细观察,您会发现 Parcel 构造函数没有对 release_date 进行“读取”操作。您可能需要添加如下内容:

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

关于java - 为日期对象实现 Parcelable - NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50848499/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com