Reputation: 159
I'm trying to select and remove categories in a RecyclerView but i got UnsupportedOperationException with some specific phone brands(Android version 8 or 9).
My code:
private var categoriesDefault: MutableList<String> = mutableListOf()
override fun onItemSelected(position: Int, isSelected: Boolean) {
if (isSelected){
categoriesDefault.add(categoriesValues[position]) // i got error here
}else{
categoriesDefault.remove(categoriesValues[position])// i got error here
}
}
Exception:
Fatal Exception: java.lang.UnsupportedOperationException
at java.util.AbstractList.add + 148(AbstractList.java:148)
at java.util.AbstractList.add + 108(AbstractList.java:108)
at com.blarma.high5.aui.base.wordsetting.CategoryFragment.onItemSelected + 19(CategoryFragment.kt:19)
at com.blarma.high5.aui.base.wordsetting.CategoryAdapter$onBindViewHolder$1.onCheckedChanged + 42(CategoryAdapter.kt:42)
at android.widget.CompoundButton.setChecked + 171(CompoundButton.java:171)
at android.widget.CompoundButton.toggle + 127(CompoundButton.java:127)
at android.widget.CompoundButton.performClick + 132(CompoundButton.java:132)
at android.view.View.performClickInternal + 6585(View.java:6585)
at android.view.View.access$3100 + 785(View.java:785)
at android.view.View$PerformClick.run + 25921(View.java:25921)
at android.os.Handler.handleCallback + 873(Handler.java:873)
at android.os.Handler.dispatchMessage + 99(Handler.java:99)
at android.os.Looper.loop + 201(Looper.java:201)
at android.app.ActivityThread.main + 6810(ActivityThread.java:6810)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 547(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main + 873(ZygoteInit.java:873)
Upvotes: 0
Views: 1218
Reputation: 320
The code seems to be correct. But the issue is in declaration. I think the mutableListOf which is generic is imposed by MutableList which is a subType of generic type. Every therefore, if you look into the documentation of kotlin. They always declare in this form mutableListOf or variable declaration as mutableListOf(variableList)
So, mutableList<T> returns MutableList<T>
Upvotes: 1