【问题标题】:Wrong state class, expecting View State but received class android.widget.CompoundButton$SavedState instead错误的状态类,期待视图状态,但收到了类 android.widget.CompoundButton$SavedState
【发布时间】:2016-05-09 10:08:53
【问题描述】:

java.lang.IllegalArgumentException: Wrong state class,期待查看状态,但收到了class android.widget.CompoundButton$SavedState。当不同类型的两个视图在同一层次结构中具有相同的 id 时,通常会发生这种情况。此视图的 id 为 id/0x2。确保其他视图不使用相同的 id。

它发生在屏幕旋转和尝试返回片段时没有重复 id plzz 有人帮助我

【问题讨论】:

标签: android android-fragments android-view


【解决方案1】:

正如问题所描述的那样,为什么会发生这种情况。

这通常发生在两个不同类型的视图具有相同的 id 时 在同一层次结构中。

确切地说,会有两种不同类型的视图具有相同的 id,这可能会在动态膨胀视图时发生。 就我而言,我有两个ViewStub 具有相同的视图idinflatedId,因此,在视图膨胀之后。应用程序进入一种状态,即视图层次结构中存在一个 ViewStub 和其他膨胀视图,具有相同的 id,从而导致崩溃。

【讨论】:

  • 在我的情况下,包括 ViewGroup 和它的孩子有相同的 id,导致崩溃
【解决方案2】:

在我的情况下,我实现 onRestoreInstanceState 未正确尝试将我的自定义 SavedState 类发送到 super.onRestoreInstanceState

我解决了它向超级呼叫发送savedState.getSuperState

【讨论】:

    【解决方案3】:

    我已经解决了....这是因为视图 ​​id 的重复,主要是当我们动态添加视图而不设置 id 时(在我的情况下,单选按钮的文本相同)

    首先检查你动态添加的所有视图

    【讨论】:

    • 我不明白,你能详细说明一下吗?
    • 有什么工具或者什么东西可以检测到这类问题吗?
    【解决方案4】:

    假设你在 Fragment 或 DialogFragment 中有一个 ScrollView,id 为:

    android:id="@+id/scrollView"
    

    您显示此 Fragment,然后更改屏幕方向,然后出现此崩溃:

    E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo { com.company.MyApp/com.company.MyActivity }: 
        java.lang.IllegalArgumentException: Wrong state class, expecting View State 
        but received class android.widget.ScrollView$SavedState instead. 
        This usually happens when two views of different type have the same id 
        in the same hierarchy. This view's id is id/scrollView. Make sure other 
        views do not use the same id.
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
      ...
    Caused by: java.lang.IllegalArgumentException: Wrong state class...
      at android.view.View.onRestoreInstanceState(View.java:13764)
      at android.support.v4.widget.NestedScrollView.onRestoreInstanceState(NestedScrollView.java:1879)
      ...
    

    解决方法是重命名您正在使用的 id,如下所示:

    android:id="@+id/scroll_view"
    

    我假设某些 id 名称已被 Android 框架中的其他视图使用,因此它们在视图状态恢复时被检测为重复名称。所以你必须改变你的身份证的名字。

    【讨论】:

      【解决方案5】:

      类似的事情发生在我身上,在我的例子中,我有一个纵向模式的布局和一个横向模式的布局。两者都有相似之处,但有两个按钮在两种布局中具有相同的 Id,但在横向布局中这两个按钮的类型不同。因此,当我将设备的方向更改为横向时,发生了类似的错误。修复是在两种布局中使用相同类型的按钮。

      【讨论】:

        【解决方案6】:

        除了我没有重复的 ID 之外,我还收到了错误消息。 我的代码是这样的:

        override fun onSaveInstanceState(): Parcelable {
            val bundle = Bundle()
            bundle.putString(SELECTED_KEY, selectedItemId)
            bundle.putParcelable(SUPER_STATE_KEY, super.onSaveInstanceState())
            return bundle
        }
        
        override fun onRestoreInstanceState(state: Parcelable) {
            if (state is Bundle) {
                selectedItemId = state.getString(SELECTED_KEY, null)
                selectedItemId?.let{
                    views.forEach {view ->
                        view.isSelected = it == view.tag
                    }
                }
            }
        
            super.onRestoreInstanceState(state)
        }
        

        我看到有一个未解决的问题here。当我像这样更改super.onRestoreInstanceState 调用参数时,我设法找到了一种不会破坏应用程序的解决方法:

        override fun onSaveInstanceState(): Parcelable {
            val bundle = Bundle()
            vgs_ring?.constraintLayoutParams()?.let {
                bundle.putParcelable(PARENT_STATE_KEY, super.onSaveInstanceState())
                bundle.putFloat(RING_POSITION_KEY, it.verticalBias)
            }
            super.onSaveInstanceState()
            return bundle
        }
        
        
        override fun onRestoreInstanceState(state: Parcelable) {
            var parentState : Parcelable? = null
            if (state is Bundle) {
                val curRingPosition = state.getFloat(RING_POSITION_KEY, DEFAULT_RING_POSITION)
                state.getParcelable<Parcelable?>(PARENT_STATE_KEY)?.let {
                    parentState = it
                }
                vgs_ring?.constraintLayoutParams()?.let {
                    it.verticalBias = curRingPosition
                    calculateRingPosition()
                }
            }
        
            super.onRestoreInstanceState(parentState ?: super.onSaveInstanceState())
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-01-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-11-26
          • 1970-01-01
          • 2021-10-01
          相关资源
          最近更新 更多