简体   繁体   中英

Reading String from parcelable object returns null

I have a class that implements Parcelable. The class has 1 int and 2 String's. When reading only the int data is returned. The first string is returned as " " and second is returned as null.

Writing parcel -

After writing int - data position 376 parcel size 376.
After writing String1 - data position 392 parcel size 392.
After writing String2 - data position 412 parcel size 412.

Reading parcel -

After reading int - data position 112 parcel size 152.
After reading String1 - data position 120 parcel size 152.
After reading String2 - data position 124 parcel size 152.

Should the data size be the same while reading parcel data ?

Class implementing Parcelable -

public class Message implements Parcelable {
private long id;
private String from;
private String message;

private String TAG = Message.class.getSimpleName();


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

    @Override
    public Message createFromParcel(Parcel source) {
        return new Message(source);
    }

    @Override
    public Message[] newArray(int size) {
        Log.v("Message", "New array  size " + size);
        return new Message[size];
    }
};

public Message(){

}

private Message(Parcel source){
    id = source.readInt();
    Log.v(TAG, "Reading parcel object data position " + source.dataPosition() + " parcel size " + source.dataSize());
    from = source.readString();
    Log.v(TAG, "Reading parcel object data position " + source.dataPosition() + " parcel size " + source.dataSize());       
    message = source.readString();
    Log.v(TAG, "Reading parcel object data position " + source.dataPosition() + " parcel size " + source.dataSize());
}

/**
 * @return the id
 */
public long getId() {
    return id;
}
/**
 * @param id the id to set
 */
public void setId(long id) {
    this.id = id;
}
/**
 * @return the from
 */
public String getFrom() {
    return from;
}
/**
 * @param from the from to set
 */
public void setFrom(String from) {
    this.from = from;
}
/**
 * @return the message
 */
public String getMessage() {
    return message;
}
/**
 * @param message the message to set
 */
public void setMessage(String message) {
    this.message = message;
}

/* (non-Javadoc)
 * @see java.lang.Object#toString()
 */
@Override
public String toString() {
    return "Message [id=" + id + ", from=" + from + ", message=" + message
            + "]";
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    Log.v(TAG, "Writing parcel object " + flags + " -- " + id + " -- " + from  + " -- " + message);
    dest.writeLong(id);
    Log.v(TAG, "Writing parcel object data position " + dest.dataPosition() + " parcel size " + dest.dataSize());
    /*dest.writeString(from);
    dest.writeString(message);*/
    dest.writeString("from");
    Log.v(TAG, "Writing parcel object data position " + dest.dataPosition() + " parcel size " + dest.dataSize());
    dest.writeString("message");
    Log.v(TAG, "Writing parcel object data position " + dest.dataPosition() + " parcel size " + dest.dataSize());
}   

}

The problem was , I was using writeLong() but while reading was doing a readInt() . Replaced the readInt() with readLong() and now it works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM