枚舉Parcelable對象在Android項目中,我有這些領域類:與Android的
public class TransactionHistoryDetail1 implements Parcelable, DatabaseEnabled{
private long id;
private static final String TABLE_NAME = "TransactionHistoryDetail";
private static final String EMPTY_STRING = "";
@XmlElement(name = "TxUd", required = true)
private String TxUd;
@XmlElement(name = "TxLclDtTm", required = true)
private Date TxLclDtTm;
@XmlElement(name = "CcyCd")
private CurrencyCode CcyCd;
@XmlElement(name = "Amt")
private BigDecimal Amt;
@XmlElement(name = "PmttpCd", required = true)
private PaymentTypeCode PmttpCd;
@XmlElement(name = "OprtnCd", required = true)
private OperationCode OprtnCd;
@XmlElement(name = "AppLabltpCd", required = true)
private AppLabelTypeCode AppLabltpCd;
@XmlElement(name = "PdSrl")
private String PdSrl;
@XmlElement(name = "PdBrndDsc")
private String PdBrndDsc;
@XmlElement(name = "UsrEml")
private String UsrEml;
@XmlElement(name = "CstmrAdr")
private String CstmrAdr;
@XmlElement(name = "TxDtlDsc")
private String TxDtlDsc;
@XmlElement(name = "TxRltdInd")
private boolean TxRltdInd;
@XmlElement(name = "TxSttsCd", required = true)
private TransactionStatusCode TxSttsCd;
@XmlElement(name = "UpdtDt", required = true)
private Date UpdtDt;
...
}
我試着去寫和讀這個類的對象作爲Parcelables並但林不知道如何寫和讀枚舉。
我writeToParcel方法是這樣的:
@Override
public void writeToParcel(Parcel dest, int flags) {
SimpleDateFormat sdf = new SimpleDateFormat(JsonBuilder.DateFormat);
dest.writeLong(id);
dest.writeDouble(Amt.doubleValue());
dest.writeString(PdSrl);
dest.writeString(PdBrndDsc);
dest.writeString(TxUd);
dest.writeString(PdSrl);
dest.writeString(UsrEml);
dest.writeString(CstmrAdr);
dest.writeString(TxDtlDsc);
dest.writeString((CcyCd == null) ? "" : CcyCd.name());
dest.writeString((PmttpCd == null) ? "" : PmttpCd.name());
dest.writeString((OprtnCd == null) ? "" : OprtnCd.name());
dest.writeString((AppLabltpCd == null) ? "" : AppLabltpCd.name());
dest.writeString(sdf.format(TxLclDtTm));
dest.writeString(sdf.format(UpdtDt));
dest.writeByte((byte) (TxRltdInd ? 1 : 0));
}
和我的包裹構造看起來像這樣
私人TransactionHistoryDetail1(包裹中){
SimpleDateFormat sdf = new SimpleDateFormat(JsonBuilder.DateFormat);
try {
TxLclDtTm = sdf.parse(in.readString());
UpdtDt = sdf.parse(in.readString());
} catch (ParseException e) {
e.printStackTrace();
}
id = in.readLong();
TxUd = in.readString();
PdSrl = in.readString();
PdBrndDsc = in.readString();
UsrEml = in.readString();
CstmrAdr = in.readString();
TxDtlDsc = in.readString();
TxRltdInd = in.readByte() != 0;
Amt = new BigDecimal(in.readDouble());
CcyCd = CurrencyCode.valueOf(in.readString());
PmttpCd = PaymentTypeCode.valueOf(in.readString());
OprtnCd = OperationCode.valueOf(in.readString());
AppLabltpCd = AppLabelTypeCode.valueOf(in.readString());
TxSttsCd = TransactionStatusCode.fromValue(in.readString());
}
的writeToParcel我相信的其工作好吧,但構造函數崩潰在「CcyCd」行。
我的貨幣代碼類是一個枚舉,(所以都PaymentTypeCode,操作代碼和AppLabelTypeCode),看起來像這樣:
@XmlType(name = "CurrencyCode")
@XmlEnum
public enum CurrencyCode {
EUR;
public String value() {
return name();
}
public static CurrencyCode fromValue(String v) {
return valueOf(v);
}
}
有另一種方式來應對Parcelables並枚舉?
的例外,我得到的是:
了java.lang.RuntimeException:無法啓動活動ComponentInfo {包/ package.activities.ChildActivity}:java.lang.IllegalArgumentException異常:無枚舉常量package.data .apiClasses.CurrencyCode.EURCASHCLSAPP2017-04-10T09:07:52.525Z2017-04-10T09 :07:52.528ZCHILD_12345?
什麼是tmpMState? – Ric
林猜測它的tmpCurrencyCode。在in.readInt()已經有0,101,24等等之後,我在tmpCurrencyCode中獲得了一些奇怪的值。在這之後,我得到一個異常的行下面無法啓動活動ComponentInfo {package/package.activities.ChildActivity}:java.lang.ArrayIndexOutOfBoundsException:length = 1; index = 24 – Ric
你是否按照閱讀和書寫包裹的順序? 你應該完全按照你寫給它的順序閱讀包裹,但是在你的代碼中,你不要按順序去做。 –