我得到了这个例外:
06-09 13:14:41.917: E/AndroidRuntime(630): java.util.ConcurrentModificationException
06-09 13:14:41.917: E/AndroidRuntime(630): at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
我使用以下EndlessAdapter
:
private class DemoAdapter extends EndlessAdapter {
private RotateAnimation rotate=null;
DemoAdapter(ArrayList<Outlets> outletList) {
super(new OutletListAdapter(VerticalsListActivity.this, outletList));
rotate=new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rotate.setDuration(600);
rotate.setRepeatMode(Animation.RESTART);
rotate.setRepeatCount(Animation.INFINITE);
}
@Override
protected View getPendingView(ViewGroup parent) {
View row=getLayoutInflater().inflate(R.layout.row, null);
View child=row.findViewById(android.R.id.text1);
child.setVisibility(View.GONE);
child=row.findViewById(R.id.throbber);
child.setVisibility(View.VISIBLE);
child.startAnimation(rotate);
return(row);
}
@Override
protected boolean cacheInBackground() throws Exception {
// TODO Auto-generated method stub
if(isMoreLoading){
try {
if(Project.isFilterEnabled){
getDownloadedData = (FetchAndParseData) new FetchAndParseData().execute(Project.searchUrl+"/pageno/"+ ++i);
}else{
getDownloadedData = (FetchAndParseData) new FetchAndParseData().execute(UrlConstants.OUTLETS_INFO_URL+Project.currentCityId+"/pageno/"+ ++i+"/verticalid/1");
}
inputStream = getDownloadedData.get();
XmlUtilities.parseAndLoadData(inputStream , mOutletXmlHandler);
outletList = Project.getOutletList();//Project.getOutletList();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return isMoreLoading;
}
@Override
protected void appendCachedData() {
// TODO Auto-generated method stub
adptr = (OutletListAdapter) getWrappedAdapter();
if(outletList.size()==20){
isMoreLoading=true;
}else{
isMoreLoading=false;
}
if(outletList!=null){
for (Outlets addThis : outletList) {
adptr.add(addThis);
}
}
}
}
现在,当一个列表包含少于20个元素并且我们滚动该列表时,它会抛出提到的异常。我做错了什么?
答案 0 :(得分:4)
阿伦,
当多个线程修改集合时发生ConcurrentModificationException。您的cacheInBackground()
让我相信您确实正在利用其他线程来获取您的数据。大多数情况下,当您在更改集合(例如排序)的同时添加或从集合中删除(例如缓存)时会发生这种情况。
解决此问题的最简单方法是在另一个线程(提取)中修改它之前复制该集合。修改完成后,复制新集并删除旧集。这允许您根据需要更改当前设置(在后台),但在保持响应的同时根据需要进行UI更改。
希望这有帮助,
FuzzicalLogic