解析Android中的活动之间的异常

时间:2015-02-24 11:05:10

标签: java android parcelable

这或多或少是我的问题......我有一个从Exception延伸的课程,我需要将它插入一个包裹中。

我怎样才能做到这一点?

public class Result implements Parcelable
{
    public Result(UserException exception){
        this.exception = exception;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
    if (exception != null){
        // What to do here  ant to un-parcel it?
    }
    }
}

我的课程:

public class UserException extends Exception
{
string message;
public UserException() {
    super();
}

public UserException(String message, Throwable cause)
{
    super(message, cause);

    this.cause = cause;
    this.message = message;
}
}

1 个答案:

答案 0 :(得分:1)

@Override
public void writeToParcel(Parcel dest, int flags) {
   if (exception != null){
       dest.writeParcelable(exception, flags);
   }
}

在例外中:

public class UserException extends Exception implements Parcelable{
   //Lot of fun to be Parcelable
}

你可以这样读:

exception = (UserException)in.readParcelable(UserException.class.getClassLoader());

或者更喜欢这个

exception = in.<UserException>readParcelable(UserException.class.getClassLoader());