有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何保存自定义微调器状态

我扩展了安卓。小装置。旋转器。并在我的实现中添加了一个int字段。现在我想让它保存方向改变时的字段值。我的第一个想法是使用Bundle对象:

override fun onSaveInstanceState(): Parcelable {
    val bundle = Bundle()
    bundle.putParcelable(SUPER_STATE, super.onSaveInstanceState())
    bundle.putInt(PREV_ITEM, this.prevItem) // ... save stuff
    return bundle
}

override fun onRestoreInstanceState(state: Parcelable?) {
    val newState: Parcelable
    if (state is Bundle) {
        this.prevItem = state.getInt(PREV_ITEM) // ... load stuff
        newState = state.getParcelable<Parcelable>(SUPER_STATE)
        super.onRestoreInstanceState(newState)
    }
    super.onRestoreInstanceState(state)
}

但我有一个错误:

java.lang.ClassCastException: 安卓.os.Bundle cannot be cast to 安卓.widget.Spinner$SavedState

所以我找到了Spinner source code,并发现我必须扩展内部静态类SavedState,并使用它来保存字段值。但我没能做到。Android Studio建议它“无法解析符号‘保存状态’”。 enter image description here

那么,如何保存自定义微调器的状态呢


共 (0) 个答案