Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 尝试将元素添加到可变列表时出现UnsupportedOperationException_Android_Kotlin - Fatal编程技术网

Android 尝试将元素添加到可变列表时出现UnsupportedOperationException

Android 尝试将元素添加到可变列表时出现UnsupportedOperationException,android,kotlin,Android,Kotlin,我试图在一个RecyclerView中选择和删除类别,但我得到了一些特定手机品牌(Android版本8或9)的不支持操作异常 我的代码: private var categoriesDefault: MutableList<String> = mutableListOf() override fun onItemSelected(position: Int, isSelected: Boolean) { if (isSelected){ ca

我试图在一个RecyclerView中选择和删除类别,但我得到了一些特定手机品牌(Android版本8或9)的不支持操作异常

我的代码:

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
        }
    }

代码似乎是正确的。但问题在于宣言。我认为泛型的MutableList是由泛型类型的子类型MutableList强加的。因此,如果您查看kotlin的文档,就会发现。它们总是以这种形式声明mutableListOf或变量声明为mutableListOf(variableList)


所以,mutableList返回mutableList

很抱歉,我是这个kotlin的新手,我应该在代码中的什么地方更改?
private val categoriesByDefault=mutableListOf()
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)