getParcelableArrayList 元素的最后一个显示为空字符串

The last of the getParcelableArrayList element appears as empty string

当我将 parcelable arraylist 放入包中并使用 getParcelableArrayList 获取它时,所有元素都正确显示,而最后一个元素显示为空字符串。

我的自定义 Parcelable 项目:

public class Media implements Parcelable {

    private final Uri uri;
    private Type type;
    private InfoModel infoModel;

    public Media(Uri uri, Type type) {
     
        this.uri = uri;
        this.type = type;
        this.infoModel = InfoModel.empty();
    }

    public Media() {
        this.uri = null;
        this.type = Type.image;
        this.infoModel = InfoModel.empty();
    }

    protected Media(Parcel in) {
        uri = in.readParcelable(Uri.class.getClassLoader());
        type = Type.valueOf(in.readString());
        infoModel = in.readParcelable(InfoModel.class.getClassLoader());
    }

    public void setInfoModel(InfoModel infoModel) {
        this.infoModel = infoModel;
    }

    public InfoModel getInfoModel() {
        return infoModel;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(uri, flags);
        dest.writeString(this.type.name());
        dest.writeParcelable(infoModel, flags);
    }

    public static final Creator<Media> CREATOR = new Creator<Media>() {
        @Override
        public Media createFromParcel(Parcel in) {
            return new Media(in);
        }

        @Override
        public Media[] newArray(int size) {
            return new Media[size];
        }
    };

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Media media = (Media) o;

        return uri.equals(media.uri);
    }

    @Override
    public int hashCode() {
        return uri.hashCode();
    }
}

当我检查 bundle 中的媒体列表时,发现没有问题 -> bundleIntent 函数:

public class ShowMediaActivity extends Activity {

    public static Intent buildIntent(@NonNull ArrayList<Media> medias) {
            Intent intent = new Intent(this, ShowMediaActivity.class);
            Bundle b = new Bundle();
            b.putParcelableArrayList(KEY_MEDIA, medias);
            intent.putExtra("bundle", b);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            return intent;
        }

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

        Bundle b = getIntent().getBundleExtra("bundle");
        List<Media> initialMedia = new ArrayList<>();
        if (b != null) {
            initialMedia = b.getParcelableArrayList(KEY_MEDIA);
        }

     }

onCreate 中的初始媒体元素:

initialMedia has 1 elements : Media

initialMedia has 2 elements : Media, ""

initialMedia has 3 elements : Media, Media, ""

initialMedia has 4 elements : Media, Media, Media, ""

我在 in.readInt() 而不是 in.readLong() 中留下了我在 InfoModel class 中转换为 long 的项目。我根本不知道。感谢您的帮助@emandt

    public InfoModel(Parcel in) {
        startTime = in.readLong();
        endTime = in.readLong();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(startTime);
        dest.writeLong(endTime);
    }