如何通过意图传递View的对象?

时间:2014-11-08 13:04:45

标签: java android android-intent

我尝试用Parceable实现我的POJO类。但是在向View的对象写入值时会显示错误。

POJO班级:

import android.os.Parcel;
import android.os.Parcelable;
import android.view.View;
import android.widget.EditText;


public class FieldData implements Parcelable {
private Integer id;
private String value;
private Integer job_transaction_id;
private Integer field_attribute_master_id;
private Boolean required;
private View view;
private String viewType;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

public Integer getJob_transaction_id() {
    return job_transaction_id;
}

public void setJob_transaction_id(Integer job_transaction_id) {
    this.job_transaction_id = job_transaction_id;
}

public Integer getField_attribute_master_id() {
    return field_attribute_master_id;
}

public void setField_attribute_master_id(Integer field_attribute_master_id) {
    this.field_attribute_master_id = field_attribute_master_id;
}

public Boolean getRequired() {
    return required;
}

public void setRequired(Boolean required) {
    this.required = required;
}

public View getView() {
    return view;
}

public void setView(View view) {
    this.view = view;
}

public String getViewType() {
    return viewType;
}

public void setViewType(String viewType) {
    this.viewType = viewType;
}



protected FieldData(Parcel in) {
    id = in.readByte() == 0x00 ? null : in.readInt();
    value = in.readString();
    job_transaction_id = in.readByte() == 0x00 ? null : in.readInt();
    field_attribute_master_id = in.readByte() == 0x00 ? null : in.readInt();
    byte requiredVal = in.readByte();
    required = requiredVal == 0x02 ? null : requiredVal != 0x00;
    view = (View) in.readValue(View.class.getClassLoader());
    viewType = in.readString();
}

public FieldData() {
    // TODO Auto-generated constructor stub
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    if (id == null) {
        dest.writeByte((byte) (0x00));
    } else {
        dest.writeByte((byte) (0x01));
        dest.writeInt(id);
    }
    dest.writeString(value);
    if (job_transaction_id == null) {
        dest.writeByte((byte) (0x00));
    } else {
        dest.writeByte((byte) (0x01));
        dest.writeInt(job_transaction_id);
    }
    if (field_attribute_master_id == null) {
        dest.writeByte((byte) (0x00));
    } else {
        dest.writeByte((byte) (0x01));
        dest.writeInt(field_attribute_master_id);
    }
    if (required == null) {
        dest.writeByte((byte) (0x02));
    } else {
        dest.writeByte((byte) (required ? 0x01 : 0x00));
    }
    **dest.writeValue(view);**//Error at this line
    dest.writeString(viewType);
}

@SuppressWarnings("unused")
public static final Parcelable.Creator<FieldData> CREATOR = new Parcelable.Creator<FieldData>()      {
    @Override
    public FieldData createFromParcel(Parcel in) {
        return new FieldData(in);
    }

    @Override
    public FieldData[] newArray(int size) {
        return new FieldData[size];
    }
};

}

dest.writeValue(view);

显示

错误

错误:java.lang.RuntimeException:Parcel:无法编组值android.widget.EditText@41ac34a8

2 个答案:

答案 0 :(得分:2)

View不可分割。

理想情况下,您可以在第二个活动中创建类似的视图。所需的属性可以通过意图从第一个活动传递到第二个活动

答案 1 :(得分:1)

通过在pojo类中实现Serializable接口使其可序列化。 那么你可以将Serializable放入Bundle。

public final class SavingsAccount implements Serializable {
{

}