有很多类似的问题,但仍然找不到答案。
我有一个具有动态高度的自定义视图,我想保存它。看起来很简单,并且可以在所有设备上进行测试,但是可以从Crashlytics
...
这是已保存实例的代码:
internal class SavedState : BaseSavedState {
var height = 0
constructor(source: Parcel) : super(source) {
height = source.readInt()
}
constructor(superState: Parcelable) : super(superState)
override fun writeToParcel(out: Parcel, flags: Int) {
super.writeToParcel(out, flags)
out.writeInt(height)
}
companion object {
@JvmField
val CREATOR = object : Parcelable.Creator<SavedState> {
override fun createFromParcel(source: Parcel): SavedState {
return SavedState(source)
}
override fun newArray(size: Int): Array<SavedState?> {
return arrayOfNulls(size)
}
}
}
}
保存和恢复:
override fun onSaveInstanceState(): Parcelable {
val savedState = SavedState(super.onSaveInstanceState()!!)
savedState.height = height
return savedState
}
override fun onRestoreInstanceState(state: Parcelable?) {
if (state is SavedState) {
super.onRestoreInstanceState(state.superState)
layoutParams.height = state.height
} else {
super.onRestoreInstanceState(state)
}
}
错误:
Caused by android.os.BadParcelableException
ClassNotFoundException when unmarshalling: androidx.viewpager.widget.ViewPager$SavedState
android.os.Parcel.readParcelableCreator (Parcel.java:2295)
android.view.View$BaseSavedState.<init> (View.java:20570)
com.app.utils.view.DynamicHeightViewPager$SavedState.<init> (DynamicHeightViewPager.kt:54)
com.app.utils.view.DynamicHeightViewPager$SavedState$Companion$CREATOR$1.createFromParcel (DynamicHeightViewPager.kt:69)
com.app.utils.view.DynamicHeightViewPager$SavedState$Companion$CREATOR$1.createFromParcel (DynamicHeightViewPager.kt:67)
android.os.Parcel.readParcelable (Parcel.java:2252)
在其他视图上也得到类似的错误。
有什么想法吗?