为什么会出现java.util.ConcurrentModificationException错误?
我在使用此代码时遇到问题:为什么会出现java.util.ConcurrentModificationException错误?,java,android,arraylist,android-asynctask,Java,Android,Arraylist,Android Asynctask,我在使用此代码时遇到问题: protected void onPostExecute(ArrayList<Example> examples){ for(Example i : examples) { customAdapter.add(i); } listExamples.setAdapter(customAdapter); } 编辑2:这是我的asynktask方法和doInBack
protected void onPostExecute(ArrayList<Example> examples){
for(Example i : examples)
{
customAdapter.add(i);
}
listExamples.setAdapter(customAdapter);
}
编辑2:这是我的asynktask
方法和doInBackground()中的代码
class chargeExample扩展了异步任务{
受保护的void onPreExecute(){
}
受保护的ArrayList doInBackground(无效…参数){
String url=“我的API REST的GET方法的url”;
HttpGet方法=新的HttpGet(url);
setHeader(“内容类型”、“应用程序/json”);
试一试{
HttpResponse response=httpClient.execute(方法);
String responseString=EntityUtils.toString(response.getEntity());
JSONArray responseJSON=新的JSONArray(responseString);
对于(int i=0;i从您的评论来看,您迭代的示例
列表似乎与支持customAdapter
适配器的列表相同,这意味着当您从customAdapter
添加/删除项目时,这些项目也将从示例
引用的列表中添加/删除。在这种情况下,请参阅要防止异常,可以执行以下操作:
class chargeExample extends AsyncTask<Void, Integer, ArrayList<Example>> {
protected void onPreExecute(){
}
protected ArrayList<Example> doInBackground(Void... params) {
ArrayList<Example> newList = new ArrayList<Example>();
String url = "url of my GET method of my API REST";
HttpGet method = new HttpGet(url);
method.setHeader("content-type", "application/json");
try{
HttpResponse response = httpClient.execute(method);
String responseString = EntityUtils.toString(response.getEntity());
JSONArray responseJSON = new JSONArray(responseString);
for(int i=0; i<responseJSON.length(); i++){
JSONObject object = responseJSON.getJSONObject(i);
int idMain = object.getInt("idMain");
String date = object.getString("date");
String name = object.getString("name");
double value = object.getDouble("value");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-dd");
Date datePar = sdf.parse(date);
newList.add(new Example(idMain, datePar, name, value));
}
}catch(Exception ex){
Log.e("ServicioRest", ex.toString());
}
return newList;
}
如果在另一个线程上修改列表,则可能发生此错误。防止此错误的一种方法是在synchronized
块中访问列表,例如:synchronized(示例){…}
,您可能必须对customAdapter
执行相同的操作。堆栈跟踪如何显示相同onPostExecute()的两个不同调用
method,但您发布的代码不包含任何递归调用,而且长度也不足以解释跟踪中的行号?简化问题很好,但是发布您发布的问题所伴随的任何错误。@Error404,该解释与实际堆栈跟踪不一致,但也许我们可以理解如果您在第131行附近给我们代码,我们将了解实际情况。@Error404您需要在每次访问该对象时同步该对象,而不仅仅是使用该方法。您在创建customAdapter
对象时是否使用了examp
列表?如果是,则这一行customAdapter.add(i);
将实际修改适配器使用的下划线ArrayList
(examp
),这意味着您在迭代列表时正在修改列表,因此出现异常。
class chargeExample extends AsyncTask<Void, Integer, ArrayList<Example>> {
protected void onPreExecute(){
}
protected ArrayList<Example> doInBackground(Void... params) {
String url = "url of my GET method of my API REST";
HttpGet method = new HttpGet(url);
method.setHeader("content-type", "application/json");
try{
HttpResponse response = httpClient.execute(method);
String responseString = EntityUtils.toString(response.getEntity());
JSONArray responseJSON = new JSONArray(responseString);
for(int i=0; i<responseJSON.length(); i++){
JSONObject object = responseJSON.getJSONObject(i);
int idMain = object.getInt("idMain");
String date = object.getString("date");
String name = object.getString("name");
double value = object.getDouble("value");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-dd");
Date datePar = sdf.parse(date);
examp.add(new Example(idMain, datePar, name, value));
}
}catch(Exception ex){
Log.e("ServicioRest", ex.toString());
}
return examp;
}
class chargeExample extends AsyncTask<Void, Integer, ArrayList<Example>> {
protected void onPreExecute(){
}
protected ArrayList<Example> doInBackground(Void... params) {
ArrayList<Example> newList = new ArrayList<Example>();
String url = "url of my GET method of my API REST";
HttpGet method = new HttpGet(url);
method.setHeader("content-type", "application/json");
try{
HttpResponse response = httpClient.execute(method);
String responseString = EntityUtils.toString(response.getEntity());
JSONArray responseJSON = new JSONArray(responseString);
for(int i=0; i<responseJSON.length(); i++){
JSONObject object = responseJSON.getJSONObject(i);
int idMain = object.getInt("idMain");
String date = object.getString("date");
String name = object.getString("name");
double value = object.getDouble("value");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-dd");
Date datePar = sdf.parse(date);
newList.add(new Example(idMain, datePar, name, value));
}
}catch(Exception ex){
Log.e("ServicioRest", ex.toString());
}
return newList;
}
protected void onPostExecute(ArrayList<Example> examples){
for(Example i : examples)
{
customAdapter.add(i);
}
customAdapter.notifyDataSetChanged();
}