BadParcelableException ClassNotFoundException CoordinatorLayout $ SavedState

时间:2016-03-11 18:01:17

标签: android android-support-library

我有一个自定义视图扩展CoordinatorLayout,我使用扩展BaseSavedState的类来覆盖onRestoreInstanceStateonSaveInstanceState的典型方式。 当我的观点恢复后,我得到:


Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: android.support.design.widget.CoordinatorLayout$SavedState
at android.os.Parcel.readParcelableCreator(Parcel.java:2432)
at android.os.Parcel.readParcelable(Parcel.java:2358)
at android.view.AbsSavedState.(AbsSavedState.java:57)
at android.view.View$BaseSavedState.(View.java:21457)
at android.support.design.widget.CoordinatorLayout$SavedState.(CoordinatorLayout.java:2656)

如果我评论我的两个方法onRestoreInstanceStateonSaveInstanceState,则此问题已解决。请参阅以下自定义视图代码:

 @Override
  protected Parcelable onSaveInstanceState() {
    Parcelable superState = super.onSaveInstanceState();
    SavedState ss = new SavedState(superState);
    ss.scrollX = scrollView.getScrollX();
    ss.scrollY = scrollView.getScrollY();
    return ss;
  }

  @Override
  protected void onRestoreInstanceState(Parcelable state) {
    if (!(state instanceof SavedState)) {
      super.onRestoreInstanceState(state);
      return;
    }
    final SavedState ss = (SavedState) state;
    super.onRestoreInstanceState(ss.getSuperState());

    post(() -> scrollView.scrollTo(ss.scrollX, ss.scrollY));
  }

  static class SavedState extends BaseSavedState {
    int scrollX;
    int scrollY;

    SavedState(Parcelable superState) {
      super(superState);
    }

    private SavedState(Parcel in) {
      super(in);
      this.scrollX = in.readInt();
      this.scrollY = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
      super.writeToParcel(out, flags);
      out.writeInt(this.scrollX);
      out.writeInt(this.scrollY);
    }

    //required field that makes Parcelables from a Parcel
    public static final Parcelable.Creator<SavedState> CREATOR =
        new Parcelable.Creator<SavedState>() {
          public SavedState createFromParcel(Parcel in) {
            return new SavedState(in);
          }

          public SavedState[] newArray(int size) {
            return new SavedState[size];
          }
        };
  }

1 个答案:

答案 0 :(得分:8)

这是因为AbsSavesState使用null类加载器,所以你必须自己序列化它:


@Override
  protected Parcelable onSaveInstanceState() {
    Parcelable superState = super.onSaveInstanceState();
    SavedState ss = new SavedState(superState);
    ss.scrollX = scrollView.getScrollX();
    ss.scrollY = scrollView.getScrollY();
    return ss;
  }

  @Override
  protected void onRestoreInstanceState(Parcelable state) {
    if (!(state instanceof SavedState)) {
      super.onRestoreInstanceState(state);
      return;
    }
    final SavedState ss = (SavedState) state;
    super.onRestoreInstanceState(ss.superState);

    post(() -> scrollView.scrollTo(ss.scrollX, ss.scrollY));
  }

  static class SavedState extends BaseSavedState {
    Parcelable superState;
    int scrollX;
    int scrollY;

    SavedState(Parcelable superState) {
      // We store the state ourself and send an empty state to to the parent
      super(EMPTY_STATE);
      this.superState = superState;
    }

    private SavedState(Parcel in) {
      super(in);
      this.superState = in.readParcelable(CoordinatorLayout.SavedState.class.getClassLoader());
      this.scrollX = in.readInt();
      this.scrollY = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
      super.writeToParcel(out, flags);
      out.writeParcelable(superState, flags);
      out.writeInt(this.scrollX);
      out.writeInt(this.scrollY);
    }

    //required field that makes Parcelables from a Parcel
    public static final Parcelable.Creator CREATOR =
        new Parcelable.Creator() {
          public SavedState createFromParcel(Parcel in) {
            return new SavedState(in);
          }

          public SavedState[] newArray(int size) {
            return new SavedState[size];
          }
        };
  }