为Number对象实现Parcelable时的ClassCastException

时间:2018-06-17 18:32:17

标签: java android

我正在调用TheMovieDatabase API,它返回的其中一个对象是一个数字,请参阅文档here

我正在我的Movie对象类中实现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);

2 个答案:

答案 0 :(得分:3)

尝试使用:

parcel.writeInt(vote_average.intValue());

由于错误说它无法将LazilyParsedNumber解析为Integer,因此它必须是基于this class的对象。此类提供intValue()方法,该方法将返回int。

答案 1 :(得分:2)

如果您的vote_average变量是LazilyParsedNumber,那么您需要在该变量上调用intValue()才能获得int。

vote_average.intValue();