ArrayAdapter.add-->java.lang.UnsupportedOperationException
程序员文章站
2022-06-04 10:37:17
...
[b]source code[/b]:
[b]situation[/b]:
利用ArrayAdapter.add方法添加spinner item时,抛出java.lang.[color=red]UnsupportedOperationException[/color]...
[b]root cause[/b]:[color=red]ArrayList size固定,导致ArrayAdapter也是resizeable[/color]
[b]solutions[/b]:
1.在创建ArrayAdapter时,不设置固定size的ArrayList
2.利用[color=green]arrayAdapter.addAll(String...items)[/color]或者[color=green]arrayAdapter.addAll(Collection<? extends String> collection)[/color],需API11及以上提供支持
cities = new String[] { "Android", "BlackBerry", "J2ME","Symbian","Broncho", "LinuxMobile", "Palm", "WindwosMobile" };
cityList = Arrays.asList(cities);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,cityList);
[b]situation[/b]:
利用ArrayAdapter.add方法添加spinner item时,抛出java.lang.[color=red]UnsupportedOperationException[/color]...
[b]root cause[/b]:[color=red]ArrayList size固定,导致ArrayAdapter也是resizeable[/color]
[b]solutions[/b]:
1.在创建ArrayAdapter时,不设置固定size的ArrayList
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
cities = new String[] { "Android", "BlackBerry", "J2ME","Symbian","Broncho", "LinuxMobile", "Palm", "WindwosMobile" };
for(int i=0; i<cities.length; i++){
arrayAdapter.add(cities[i]);
}
2.利用[color=green]arrayAdapter.addAll(String...items)[/color]或者[color=green]arrayAdapter.addAll(Collection<? extends String> collection)[/color],需API11及以上提供支持
推荐阅读
-
java.lang.UnsupportedOperationException 异常
-
java.lang.UnsupportedOperationException: seccomp unavailable: CONFIG_SECCOMP not compiled into kerne
-
String数组转化List数组之java.lang.UnsupportedOperationException异常问题
-
ArrayAdapter.add-->java.lang.UnsupportedOperationException
-
java.lang.UnsupportedOperationException解决方法
-
字符串操作异常-----java.lang.UnsupportedOperationException: null