我正在调用TheMovieDatabase API,其中它返回的对象之一是一个数字,请参阅文档 这里。
我正在实现可包裹性(Parcelable),并尝试相应地读写我的电影对象类:
读取:
"写"
我在尝试实现时遇到了ClassCastException错误:
更新:
我正在实现可包裹性(Parcelable),并尝试相应地读写我的电影对象类:
读取:
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();
release_date = in.readString();
vote_average = in.readInt();
}
"写"
@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.writeString(release_date);
parcel.writeInt((Integer) vote_average);
}
我在尝试实现时遇到了ClassCastException错误:
java.lang.ClassCastException: com.google.gson.internal.LazilyParsedNumber cannot be cast to java.lang.Integer
at popularmovies.troychuinard.com.popularmovies.Model.Movie.writeToParcel(Movie.java:204)
at android.os.Parcel.writeParcelable(Parcel.java:1496)
at android.os.Parcel.writeValue(Parcel.java:1402)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:724)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408)
at android.os.Bundle.writeToParcel(Bundle.java:1157)
at android.os.Parcel.writeBundle(Parcel.java:764)
at android.content.Intent.writeToParcel(Intent.java:8687)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3082)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518)
at android.app.Activity.startActivityForResult(Activity.java:4225)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:68)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:751)
at android.app.Activity.startActivity(Activity.java:4522)
at android.app.Activity.startActivity(Activity.java:4490)
at popularmovies.troychuinard.com.popularmovies.MainActivity$MyAdapter$1.onClick(MainActivity.java:202)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
更新:
更新
以下是具体内容:
parcel.writeInt((Integer) vote_average);
vote_average
是哪种类型? - Vladyslav Matviienko