![](/img/trans.png)
[英]ConcurrentModificationException in Android AsyncTask
[英]Solving ConcurrentModificationException problem with Asynctask (ANDROID)
我得到一个ConcurrentModificationException(ArrayList)异常,我不知道如何解决它,因为异常非常罕见,不可重现。
情况:我有一个主线程:
for(Element element : myList){
Value value = element.getValue(); <-------- ConcurrentModificationException (ArrayList)
}
和AsyncTask:
public class LoadMapObjectsTask extends AsyncTask<String, String, String> {
protected String doInBackground(String... params) {
// some other logic
myList.remove(element);
}
}
我能理解冲突,但我不确定如何正确地预防冲突。 也许用简单的同步{}? 我怎么解决这个问题?
也许您可以更改代码以返回特定值,该值可以被识别为使用Iterator.remove()
删除元素的符号:
Iterator<Element> it = myList.iterator();
while (it.hasNext()) {
Value value = it.next().getValue()
if (value == null) {
it.remove();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.