我遇到了这段代码的麻烦:
protected void onPostExecute(ArrayList<Example> examples){
for(Example i : examples)
{
customAdapter.add(i);
}
listExamples.setAdapter(customAdapter);
}
其中listExamples是我在mainActivity布局中的ListView
。
它给我的错误是java.util.ConcurrentModificationException
错误。我知道当你修改例如循环中的ArrayList时会发生这种情况,但在这里我并没有真正修改我的ArrayList值。我只是将Example
的对象添加到我的CustomAdapter中(它不在循环中迭代)。
注意:正如您所看到的,此代码包含在AsynkTask
方法的onPostExecute部分中。
我在互联网上查看问题,在Stackoverflow中,但我不能解决我的问题。在我看到的所有问题中,我看到他们有一个他们正在循环内修改的ArrayList,但这不是我的情况。
如果您有任何想法如何解决,或者我对上述概念有误,请告诉我。
提前致谢!
编辑:我的日志控制台的堆栈跟踪(我只通过发布错误日志简化了它)
06-05 02:22:31.891 24678-24678/ccom.example.user.project E/﹕ appName=com.example.user.project, acAppName=/system/bin/surfaceflinger
06-05 02:22:31.891 24678-24678/com.example.user.project E/﹕ 0
06-05 02:22:31.891 24678-24678/com.example.user.project E/﹕ appName=com.example.user.project, acAppName=/system/bin/surfaceflinger
06-05 02:22:31.891 24678-24678/com.example.user.project E/﹕ 0
06-05 02:22:32.953 24678-24678/com.example.user.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.user.project, PID: 24678
java.util.ConcurrentModificationException
at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
at com.example.user.project.MainActivity$chargeExample.onPostExecute(MainActivity.java:192)
at com.example.user.project.MainActivity$chargeExample.onPostExecute(MainActivity.java:131)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
编辑2:以下是我的asynktask
方法以及doInBackground()
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;
}
答案 0 :(得分:1)
从您的评论中看来,您在examples
上迭代的列表与支持customAdapter
适配器的列表相同,这意味着当您从customAdapter
添加/删除项目时还会在examples
引用的列表中添加/删除项目。在这种情况下,为了防止异常,您可以执行以下操作:
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;
}
和onPostExecute(...)
protected void onPostExecute(ArrayList<Example> examples){
for(Example i : examples)
{
customAdapter.add(i);
}
customAdapter.notifyDataSetChanged();
}