簡體   English   中英

如何通過意圖傳遞View對象?

[英]How to pass object of View via 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:宗地:無法封送值android.widget.EditText@41ac34a8

View不可分割。

理想情況下,您可以在第二個活動中創建類似的視圖。 所需服裝可以通過意圖從第一項活動傳遞到第二項活動

通過在pojo類中實現Serializable Interface使其可序列化。 然后您可以將Serializable放在Bundle中。

public final class SavingsAccount implements Serializable {
{

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM