Android使用ArrayLists时报错:java.util.ConcurrentModificationException
报错原因是同一个集合被多个线程编辑了。导致集合元素丢失
解决方法——
1、改有CopyOnWriteArrayList
private List<MqttMsgBean> mqttMsgList = new CopyOnWriteArrayList<>();
2、 remove 改用 removeAll
if (!mqttMsgList.isEmpty()) {List<MqttMsgBean> mqttMsgList2 = new ArrayList<>();for (MqttMsgBean item : mqttMsgList) {if (channel == item.param.number) {mqttMsgList2.add(item);}}mqttMsgList.removeAll(mqttMsgList2); }