在一个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));
}
我的Partru的构造函数看起来像这样
private TransactionHistoryDetail1(Parcel in){
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”行崩溃。
我的CurrencyCode类是一个枚举,(PaymentTypeCode,OperationCode和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 / package.activities.ChildActivity}:java.lang.IllegalArgumentException:No enum constantpackage.data.apiClasses.CurrencyCode. EUR CASH CLSAPP2017-04-10T09:07:52.525Z2017-04-10T09:07:52.528Z CHILD_12345?
答案 0 :(得分:1)
首先,您应该阅读您写给它的确切顺序中的Parcel
。由于Parcel只是按顺序写入和读取数据而不是实际序列化数据,所以你必须按顺序保持读写,否则你会读错值并得到错误。
最好将Enum
视为int
,将其写为parcel
,如下所示:
dest.writeInt(this.CcyCd == null ? -1 : this.CurrencyCode.ordinal());
并按照以下方式阅读:
int tmpCurrencyCode = in.readInt();
this.CcyCd = tmpCurrencyCode == -1 ? null : CurrencyCode.values()[tmpMState];
P.S:此代码也检查空值^^
答案 1 :(得分:0)
我在解析可空枚举时遇到问题。如果是这种情况,请在此处查找 https://stackoverflow.com/a/58337932/5980046