Java 属性为异常的可包裹类(Android)
我想使用类中的Parcelable将一个对象从一个活动发送到另一个活动。我有一个Parcelable类,它有两个字符串和一个异常作为属性Java 属性为异常的可包裹类(Android),java,android,parcelable,Java,Android,Parcelable,我想使用类中的Parcelable将一个对象从一个活动发送到另一个活动。我有一个Parcelable类,它有两个字符串和一个异常作为属性 public class ReportErrorVO implements Parcelable { private String titleError; private String descriptionError; private Exception exceptionError; public ReporteErro
public class ReportErrorVO implements Parcelable {
private String titleError;
private String descriptionError;
private Exception exceptionError;
public ReporteErrorVO(Parcel in) {
titleError = in.readString();
descriptionError = in.readString();
exceptionError = ????; //What do I put here?
}
public ReporteErrorVO() {
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(titleError);
dest.writeString(descriptionError);
dest.writeException(exceptionError);
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public ReportErrorVO createFromParcel(Parcel in) {
return new ReportErrorVO(in);
}
public ReportErrorVO[] newArray(int size) {
return new ReportErrorVO[size];
}
};
@Override
public int describeContents() {
return 0;
}
//Getter and setters atributes...
}
如何在parcelable属性中设置异常?您可以在.readException()中使用readException方法
,如果异常已写入地块,此方法将抛出异常,因此您可以捕获它并保存到变量中
try {
in.readException();
} catch (Exception e){
exceptionError = e;
}
请注意,此方法仅支持有限类型的异常
The supported exception types are:
* BadParcelableException
* IllegalArgumentException
* IllegalStateException
* NullPointerException
* SecurityException
* NetworkOnMainThreadException
好的,这样做有助于我工作:在数组上发送异常
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mTitleError);
dest.writeString(mDescriptionError);
Exception[] exceptions = new Exception[1];
exceptions[0] = mExceptionError;
dest.writeArray(exceptions);
}
public ReportErrorVO(Parcel in) {
mTitleError = in.readString();
mDescriptionError = in.readString();
Object[] exceptions = in.readArray(Exception.class.getClassLoader());
mExceptionError = (Exception) exceptions[0];
}
我必须在writeToParcel方法中输入什么才能写入异常?因为我使用它的方式,我得到了非法状态异常:无法执行活动的方法(我可能认为异常正在被排除)