这或多或少是我的问题...我有一个从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;
}
}

最佳答案

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

10-04 16:13