gpt4 book ai didi

android - BadParcelableException ClassNotFoundException CoordinatorLayout$SavedState

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:57:19 25 4
gpt4 key购买 nike

我有一个扩展 CoordinatorLayout 的自定义 View 我正在重写 onRestoreInstanceStateonSaveInstanceState 使用类扩展 BaseSavedState 的典型方式.当我的观点恢复时,我得到:


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 这个问题就解决了。请参阅下面的自定义 View 代码:

 @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];
}
};
}

最佳答案

这是因为 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];
}
};
}

关于android - BadParcelableException ClassNotFoundException CoordinatorLayout$SavedState,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35946908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com