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
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.
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.
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
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With