简体   繁体   English

android ConcurrentmodificationException?

[英]android ConcurrentmodificationException?

@Override
public void bleDataReceiverObserver(BluetoothDevice bluetoothDevice) {
    Log.v("bleDataReceiverObserver", bluetoothDevice.getName());
    if (list.size() == 0)
        list.add(new BleData(bluetoothDevice, bluetoothDevice.getAddress(), bluetoothDevice.getName()));
    for (BleData bleData : list) {
        if (bleData.getAddress().equals(bluetoothDevice.getAddress())) {
            break;
        } else
            list.add(new BleData(bluetoothDevice, bluetoothDevice.getAddress(), bluetoothDevice.getName()));
    }
}

Bluetooth scan results are received directly through the receiver. 蓝牙扫描结果直接通过接收器接收。 However, the speed of coming in and saving to the list is so fast that exceptions are made. 但是,进入并保存到列表的速度如此之快,以至于产生了异常。 What should I do? 我该怎么办?

From oracle docs 来自oracle文档

This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible. 当不允许对对象进行同时修改时,检测到该对象的同时修改的方法可能会引发此异常。

When accessing a List item in a loop it's not allowed to edit that list. 在循环中访问List项时,不允许编辑该列表。 You are adding item to a list in a loop. 您正在将项目添加到循环列表中。 If you really need it or you can't find any solution then use another List to add item 如果您确实需要它或找不到任何解决方案,请使用另一个List添加项目

Check this thread to learn more. 检查线程以了解更多信息。

You can use this code: 您可以使用以下代码:

  boolean isFound = false;
  for (BleData bleData : list) {
    if (bleData.getAddress().equals(bluetoothDevice.getAddress())) {
        isFound = true;
    }
  } 

  if(!isFound){
       bluetoothDevice.getAddress(), bluetoothDevice.getName()));
  }

Just use CopyOnWriteArrayList instead of ArrayList. 只需使用CopyOnWriteArrayList而不是ArrayList。 The simplest way to solve ConcurrentModificationException. 解决ConcurrentModificationException的最简单方法。

like this: 像这样:

List<String> list = new CopyOnWriteArrayList<>();

You cannot modify and iterate through same list in for loop. 您不能在for循环中修改和遍历相同的列表。 Change your method as follows: 更改您的方法,如下所示:

@Override
public void bleDataReceiverObserver(BluetoothDevice bluetoothDevice) {
    Log.v("bleDataReceiverObserver", bluetoothDevice.getName());
    if (list.size() == 0)
        list.add(new BleData(bluetoothDevice, bluetoothDevice.getAddress(), bluetoothDevice.getName()));
    for (BleData bleData : list) {
        if (bleData.getAddress().equals(bluetoothDevice.getAddress())) {
            //Do some action on match
            break;
        } 
    }
}

You're iterating through an ArrayList and modifying it at same time. 您正在遍历ArrayList并同时对其进行修改。 That causes a ConcurrentModificationException, so don't do it.Build a "shadow" copy of the list that has everything you want it to have at the end, then call removeAll() and then addAll(newList) on the original list, and modify new shadow copy in loop 这会导致ConcurrentModificationException,因此请不要这样做。构建列表的“影子”副本,该副本在末尾具有您想要的所有内容,然后调用removeAll(),然后在原始列表上调用addAll(newList),然后循环修改新卷影副本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM