Android Parcelable bad array lengths

后端 未结 1 683
遥遥无期
遥遥无期 2020-12-20 15:08

Parcelables. Why can\'t they be more easy? I\'m trying to write an application which sends an ArrayList as a parcelable. When I try to get the Intent in the second Activity,

相关标签:
1条回答
  • 2020-12-20 15:51

    You need to read from the Parcel in the same order in which you wrote things. You are writing this.id first, but you are attempting to read the string array before reading the id. Reverse the order of the calls in either the constructor or writeToParcel.

    Also, instead of writing a string array, why not just write each string individually? Seems a lot simpler.

    public NewsItem(Parcel in) {
        in.readInt();
        this.name = in.readString();
        this.bubble = in.readString();
        this.drawable = in.readString();
        this.title = in.readString();
        this.summary = in.readString();
        this.description = in.readString();
    }
    
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.id);
        dest.writeString(this.name);
        dest.writeString(this.bubble);
        dest.writeString(this.drawable);
        dest.writeString(this.title);
        dest.writeString(this.summary);
        dest.writeString(this.description);
    }
    
    0 讨论(0)
提交回复
热议问题