解析 Android 中活动之间的异常

Parsing Exceptions between Activities in Android

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

我怎样才能做到这一点?

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?
    }
    }
}

我的class:

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;
}
}
@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());