Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong state class, expecting View State but received class android.widget.CompoundButton$SavedState instead

java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.widget.CompoundButton$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/0x2. Make sure other views do not use the same id.

its occurs when screen rotate and when when trying go back to the fragment no id is repeated plzz some one help me

like image 683
Ahmed Bukhari Avatar asked May 09 '16 10:05

Ahmed Bukhari


4 Answers

As the question described why this happens.

This usually happens when two views of different type have the same id in the same hierarchy.

Exactly, there will two different types of views having the same ids, which might happen on inflating views dynamically. In my case, I have a two ViewStub with same view id and inflatedId, thus, after inflation of a view. Application went to a state that one ViewStub and other inflated view was present in the view hierarchy with the same id which result in the crash.

like image 143
Shubham Agarwal Avatar answered Nov 16 '22 15:11

Shubham Agarwal


In my case I implemented onRestoreInstanceState not correctly trying to send my custom SavedState class to super.onRestoreInstanceState

I solved it sending savedState.getSuperState to the super call.

like image 35
A. Kazarovets Avatar answered Nov 16 '22 15:11

A. Kazarovets


i have resolve it .... its because of duplication of ids of view mostly when we add view dynamically and not set ids (with same text of radio button in my case)

to resolve first check all views those u are adding dynamically

like image 3
Ahmed Bukhari Avatar answered Nov 16 '22 15:11

Ahmed Bukhari


I was just dealing with this issue when working with customViews and parcelable, trying to persist the state of my view with onSaveInstanceState() and onRestoreInstanceState(), and I was getting this same error. However I found a work around when I changed the Implementation of these above functions. I will share the before and after.

This was my before. The difference is slight.

 override fun onSaveInstanceState(): Parcelable {
    val bundle = Bundle()
    bundle.putParcelable("superState", super.onSaveInstanceState())
    bundle.putParcelableArrayList("boxState", ArrayList(boxen))

    super.onSaveInstanceState()
    return bundle
}

override fun onRestoreInstanceState(state: Parcelable) {
    var viewState = state
    if (viewState is Bundle) {
        boxen = viewState.getParcelableArrayList("boxState")!!
        viewState = viewState.getParcelable("superState")!!
    }
    val test = boxen
    Log.i(TAG, "Bundle received: $boxen and $test and finally $viewState")

    super.onRestoreInstanceState(state)
}

And this is my after.

override fun onSaveInstanceState(): Parcelable {
    val bundle = Bundle()
    bundle.putParcelable("superState", super.onSaveInstanceState())
    bundle.putParcelableArrayList("boxState", ArrayList(boxen))

    super.onSaveInstanceState()
    return bundle
}

override fun onRestoreInstanceState(state: Parcelable) {
    var viewState = state
    if (viewState is Bundle) {
        boxen = viewState.getParcelableArrayList("boxState")!!
        viewState = viewState.getParcelable("superState")!!
    }
    val test = boxen
    Log.i(TAG, "Bundle received: $boxen and $test and finally $viewState")

    super.onRestoreInstanceState(viewState)
}

Somehow all I had to do was return viewState to the superClass instead of state. I think its because the "state of the view" had not been recovered yet so to speak. We were passing state parameter and at that moment, according to the error we received, it was only a raw bundle. We had not yet extracted and assigned the actual view state(which happens in the if block) we stashed in the first function, that is why the error says "Wrong state class" and that was also why using viewState worked. I hope this helps!

like image 3
Daniel Iroka Avatar answered Nov 16 '22 16:11

Daniel Iroka