今天我遇到了一個奇怪的錯誤。這花了我很多時間。Arraylist發生了一個奇怪的錯誤
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("list people size:");
System.out.println(listPeople.size());
listPeople.clear();
ArrayList<PeopleModel> newList = new ArrayList<PeopleModel>();
newList = smsUtils.getPeopleList();
// newList.size() = 42.
for(int i=0;i<newList.size();i++){
System.out.println(i);
listPeople.add(newList.get(i));
}
//**strange error happened in loop above. I don't know why**
// **it loops to infinity and causes outofmemory.**
System.out.println("list new people size:");
System.out.println(newList.size());
if(adapter!=null){
adapter.notifyDataSetChanged();
}
}
});
}
以前,我用:
listPeople = smsUtils.getPeopleList();
但另一個奇怪的錯誤發生。 listPeople
現在有84個元素,而不是42.
謝謝!
你嘗試'的for(int i = 0,1 = newList.size(); I < l; i ++)'? 你也可以使用'listPeople.addAll(newList)'將一個數組的所有元素複製到另一個' – TulaGingerbread