我正在尝试获取List的大小但是在执行此操作时,我在size()方法上遇到了ConcurrentModificationException。这很奇怪,因为我不是在迭代它时尝试修改列表。这是我的一些代码可能导致它但我无法看到它的问题。
主要方法:
private void setupCharacterGrid() {
currentCharacters.add(0, new Character("add", "add"));
if (currentCharacters.size() < 9) {
sub(0, currentCharacters.size(), currentCharacters.size());
} else {
sub(0, 9, 9);
}
fillGrids();
charPager.setAdapter(new CustomPagerAdapter(characterGrids));
charPager.setCurrentItem(0);
}
子方法(填充列表的方法)请注意,divideCharList的类型为:List&gt;其中Character是自定义POJO。
private void sub(int start, int end, int n) {
int size = currentCharacters.size();
if (start > size) {
return;
} else if (start == size) {
// new list(currentcharacters.get(start))
// check if it fits on the previous grid
List<Character> lastGrid = dividedCharList.get(dividedCharList
.size() - 1);
if (lastGrid.size() < 9) {
lastGrid.add(currentCharacters.get(start - 1));
} else {
List<Character> finalCharList = new ArrayList<Character>();
finalCharList.add(currentCharacters.get(start - 1));
dividedCharList.add(finalCharList);
}
return;
} else {
if (size < start + n) {
sub(start, end, n--);
} else {
// new list(currentcharacters.sublist(start, end);
// end = end + n;
// start = end + 1
dividedCharList.add(currentCharacters.subList(start, end));
start = end + 1;
end = end + n;
sub(start, end, n);
}
}
}
尝试获取大小时发生异常的方法:
private synchronized void fillGrids() {
for (int i = 0; i < dividedCharList.size(); i++) {
GridView gridView = new GridView(this);
gridView.setNumColumns(3);
CharacterGridAdapter aa = new CharacterGridAdapter(this,
R.layout.charactergrid_cell, dividedCharList.get(i));
gridView.setAdapter(aa);
characterGrids.add(gridView);
}
}
请注意,当我尝试通过列表迭代并尝试获取值并将其显示在日志
时,也会发生异常修改
我刚刚注意到调用gridView.setAdapter()时发生异常,ArrayAdapter的getCount方法抛出ConcurrentModificationException
EDIT2
这是堆栈跟踪,它可能会有所帮助:
01-24 13:03:44.942: E/AndroidRuntime(15336): FATAL EXCEPTION: main
01-24 13:03:44.942: E/AndroidRuntime(15336): java.util.ConcurrentModificationException
01-24 13:03:44.942: E/AndroidRuntime(15336): at java.util.AbstractList$SubAbstractList.size(AbstractList.java:360)
01-24 13:03:44.942: E/AndroidRuntime(15336): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
01-24 13:03:44.942: E/AndroidRuntime(15336): at android.widget.GridView.setAdapter(GridView.java:182)
01-24 13:03:44.942: E/AndroidRuntime(15336): at xx.ProfileActivity.fillGrids(ProfileActivity.java:111)
01-24 13:03:44.942: E/AndroidRuntime(15336):
xx.ProfileActivity.setupCharacterGrid(ProfileActivity.java:96)
01-24 13:03:44.942: E/AndroidRuntime(15336): at xx.ProfileActivity.access$6(ProfileActivity.java:87)
根据要求提供适配器代码
public class CharacterGridAdapter extends ArrayAdapter<Character> {
private Context context;
private List<Character> objects;
private int resource;
public CharacterGridAdapter(Context context, int resource, List<Character> listChar) {
super(context, resource);
this.context = context;
this.objects = listChar;
this.resource = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
rowView = inflater.inflate(R.layout.charactergrid_cell, null);
} else {
}
return rowView;
}
}
任何可以帮我解决这个问题的人?