【问题标题】:Is there something wrong with my Parcelable class?我的 Parcelable 课程有问题吗?
【发布时间】:2013-04-21 19:45:39
【问题描述】:

我想知道我的课程是否有问题,因为我无法将额外的(由 Note 对象组成)从一个活动传递到另一个活动

这是我的 Parcelable 类的代码:注意:

public class Note implements Parcelable {

    public static int compteur = 0;
    long id;
    String summary;
    String details;

    public Note(String summary, String details) {
        super();
        compteur = compteur + 1;
        this.id = compteur;
        this.summary = summary;
        this.details = details;
    }

    public Note() {
        super();
    }

    ///// getters and settters /////

    @Override
    public String toString() {
        return "Note " + id + " (" + summary + ") --> Details: " + details;
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int arg1) {
        // TODO Auto-generated method stub
        dest.writeLong(id);
        dest.writeString(summary);
        dest.writeString(details);

    }

    public static final Parcelable.Creator<Note> CREATOR = new Parcelable.Creator<Note>() {

        @Override
        public Note createFromParcel(Parcel in) {
            // TODO Auto-generated method stub
            return new Note(in);
        }

        public Note[] newArray(int size) {
            return new Note[size];
        }
    };

    private Note(Parcel in) {
        this.id = in.readInt();
        this.summary = in.readString();
        this.details = in.readString();
    }
}

或者问题可能出在其他地方??

【问题讨论】:

    标签: java android android-intent parcelable


    【解决方案1】:

    尝试让你的 Note 构造函数接受 Parcel 公开。

      public Note(Parcel in) {
            this.id = in.readInt();
            this.summary = in.readString();
            this.details = in.readString();
        }
    

    【讨论】:

    • 我已将其更改为公开,但不幸的是它不起作用:(
    • 根据您的问题,我是否正确得知您正试图将 Note Parcel 从一项活动传递到另一项活动?
    • 您的代码在传递和读取该笔记包时是什么样的?它应该类似于 Intent i = new Intent(this, MyActivity.class); i.putExtra("name.of.your.package.Note", noteObject);Bundle b = getIntent().getExtras(); Note noteObject = b.getParcelable("name.of.your.package.Note");
    • 这里是我的问题的相关主题[链接]stackoverflow.com/questions/16133801/…
    • 我正在探索所有的可能性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多