Error While Passing An Object From An Activity To Another (Using Parcelable)

后端 未结 1 1171
深忆病人
深忆病人 2021-01-18 06:59

I Tried To Pass An Object Class From An Activity To Another Using Parcelable

I\'ve Create A Class And Name it Student

package com.example.test08_pas         


        
1条回答
  •  醉梦人生
    2021-01-18 07:10

    You need to implement the code to write and read your class fields from parcel.

    In writeToParcel:

    @Override
    public void writeToParcel(Parcel dest, int flags) {
          dest.writeString(st_AcadimicNumber);
          dest.writeString(st_Name);
          dest.writeString(st_Class);
    }
    

    The Parcel CREATOR:

    public static final Parcelable.Creator CREATOR
    = new Parcelable.Creator() {
        public Student createFromParcel(Parcel in) {
            return new Track(in);
        }
    
        public Student[] newArray(int size) {
            return new Track[size];
        }
    };
    

    And the constructor:

    public Student (Parcel source){
          /*
           * Reconstruct from the Parcel. Keep same order as in writeToParcel()
           */
          st_AcadimicNumber = source.readString();
          st_Name = source.readString();
          st_Class = source.readString();
    }   
    

    And it's done.

    Regards

    0 讨论(0)
提交回复
热议问题