现象
1 | List<String> requestPermission = Arrays.asList(permissions); |
运行抛出以下异常1
2
3
4
5Caused by: java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:161)
at java.util.AbstractList$Itr.remove(AbstractList.java:374)
at java.util.AbstractCollection.remove(AbstractCollection.java:293)
at com.tencent.edu.demo.BaseActvity.checkPermissions(BaseActvity.java:70)
原因剖析
Arrays.asList返回的是固定大小的List1
Returns a fixed-size list backed by the specified array.
不能增删数据,不能改变改List的结构;
解决办法
可以先创建ArrayList,然后在通过addAll接口赋值;1
2
3
4
5
6
7
8List<String> requestPermission = new ArrayList<>();
requestPermission.addAll(Arrays.asList(permissions));
for (String p : requestPermission) {
if (checkPermissionGranted(p)) {
requestPermission.remove(p);
}
}
ArrayList也可以是LinkedList
至此,问题可解。