简体   繁体   English

从parcelable对象读取字符串返回null

[英]Reading String from parcelable object returns null

I have a class that implements Parcelable. 我有一个实现Parcelable的类。 The class has 1 int and 2 String's. 该类有1个int和2个String。 When reading only the int data is returned. 读取时只返回int数据。 The first string is returned as " " and second is returned as null. 第一个字符串返回为“”,第二个字符串返回为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 - 实现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() . 问题是,我使用的是writeLong()但是在读取时正在执行readInt()。 Replaced the readInt() with readLong() and now it works fine. 用readLong()替换readInt(),现在它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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