Android的例外:ConcurentModificationException [英] Android Exception : ConcurentModificationException
问题描述
我得到这个例外不一致,而在后面的preSS的ListView加载从项目的详细信息,这是我们选择的。
在我这个列表视图页面上会它永远不会抛出异常,但我会去到这个列表视图项,并删除/更新列表视图。
这是我的code
如果(结果!= NULL){
尝试{
ListParser listParser =新ListParser(背景);
JSONObject的jsonParseData =新的JSONObject(结果);
JSONObject的listJson =新的JSONObject(jsonParseData.getString(GetAllResult)); 如果(listJson.getBoolean(getResources()。的getString(R.string.success))){ ** listParser.getListData(listJson); // **解析器类解析数据到ArrayList中 的String [] = ITEMS新的String [ListParser.listData.size()]; 的for(int i = 0; I< ITEMS.length;我++){
。项目[I] = ListParser.listData.get(I).getName()的toString();
mItemsID.add(ListParser.listData.get(ⅰ).getNameId()的toString());
} // Array对ArrayList的
mItems =新的ArrayList<串GT;(Arrays.asList(项目));
mListSectionPos =新的ArrayList<整数GT;();
mListItems =新的ArrayList<串GT;(); **同步(ListParser.listData){
新Poplulate()执行(ListParser.listData);
//解析器类从ArrayList中使用的数据...我认为异常是从这里扔。
} ** }其他{
CommonUtil.showValidationAlert(上下文,listJson.getString(getResources()的getString(R.string.error))。);
Log.i(TAG,+ listJson.getString(getResources()的getString(R.string.error)));
}
}赶上(JSONException E){
e.printStackTrace();
}
}其他{
ToastUtil.ToastShort(背景下,服务器超时);
}
填充的AsyncTask code,从这里去:
私有类Poplulate扩展的AsyncTask< ArrayList的< ListModel的>中太虚,太虚> { ... @覆盖
在preExecute保护无效(){
//显示加载指示器
showLoading(mListView,mLoadingView,mEmptyView);
super.on preExecute();
} @覆盖
保护无效doInBackground(ArrayList的<&的ListModel GT; ... PARAMS){
mListItems.clear();
mListSectionPos.clear();
ArrayList的<&的ListModel GT;项=参数[0];
如果(mItems.size()大于0){ //不要忘了数组排序
Collections.sort(ListParser.listData,新CustomComparator()); 字符串prev_section =; 同步(本){
对于(ListModel的current_item:项目){
串current_section = current_item.getName()子(0,1).toUpperCase(Locale.getDefault())。 如果(!prev_section.equals(current_section)){
mListItems.add(current_section);
mListItems.add(current_item.getName());
//阵列部分职位列表
mListSectionPos.add(mListItems.indexOf(current_section));
prev_section = current_section;
}其他{
mListItems.add(current_item.getName());
}
}
}
}
返回null;
} @覆盖
保护无效onPostExecute(虚空结果){
如果(!isCancelled()){
如果(mListItems.size()&下; = 0){
showEmptyText(mListView,mLoadingView,mEmptyView);
}其他{
setListAdaptor();
showContent(mListView,mLoadingView,mEmptyView);
}
}
super.onPostExecute(结果);
}
}
这里是堆栈跟踪行
03-24 18:23:24.681:E / AndroidRuntime(6841):致命异常:AsyncTask的#1
03-24 18:23:24.681:E / AndroidRuntime(6841):工艺:com.smart.adlware,PID:6841
03-24 18:23:24.681:E / AndroidRuntime(6841):了java.lang.RuntimeException:执行doInBackground发生错误()
03-24 18:23:24.681:E / AndroidRuntime(6841):在android.os.AsyncTask $ 3.done(AsyncTask.java:300)
03-24 18:23:24.681:E / AndroidRuntime(6841):在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
03-24 18:23:24.681:E / AndroidRuntime(6841):在java.util.concurrent.FutureTask.setException(FutureTask.java:222)
03-24 18:23:24.681:E / AndroidRuntime(6841):在java.util.concurrent.FutureTask.run(FutureTask.java:242)
03-24 18:23:24.681:E / AndroidRuntime(6841):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)
03-24 18:23:24.681:E / AndroidRuntime(6841):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-24 18:23:24.681:E / AndroidRuntime(6841):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)
03-24 18:23:24.681:E / AndroidRuntime(6841):在java.lang.Thread.run(Thread.java:841)
03-24 18:23:24.681:E / AndroidRuntime(6841):因:java.util.ConcurrentModificationException
03-24 18:23:24.681:E / AndroidRuntime(6841):在java.util.ArrayList中$ ArrayListIterator.next(ArrayList.java:573)
03-24 18:23:24.681:E / AndroidRuntime(6841):在com.smart.adlware.CaregiverList $ Populate.doInBackground(CaregiverList.java:324)
03-24 18:23:24.681:E / AndroidRuntime(6841):在com.smart.adlware.CaregiverList $ Populate.doInBackground(CaregiverList.java:1)
03-24 18:23:24.681:E / AndroidRuntime(6841):在android.os.AsyncTask $ 2.call(AsyncTask.java:288)
03-24 18:23:24.681:E / AndroidRuntime(6841):在java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-24 18:23:24.681:E / AndroidRuntime(6841):... 4个
所以,请尽快帮助我。我坚持这个异常。
同步(本){
对于(ListModel的current_item:项目){
串current_section = current_item.getName()子(0,1).toUpperCase(Locale.getDefault())。 如果(!prev_section.equals(current_section)){
mListItems.add(current_section);
mListItems.add(current_item.getName());
//阵列部分职位列表
mListSectionPos.add(mListItems.indexOf(current_section));
prev_section = current_section;
}其他{
mListItems.add(current_item.getName());
}
}
}
同步问题(本)
。 因为你没有提供堆栈跟踪,我不知道该异常是由操作抛出。
例如,如果你的异常是由多线程访问的项目引起的,只需更换这个
与项目
。
I am getting this exception inconsistently while listview loading on back press from details of the item, which we selected.
when I am going on this listview page it never throws exception but When I will go into this listview item and delete/Update listview.
Here is my code.
if (result != null) {
try {
ListParser listParser = new ListParser(context);
JSONObject jsonParseData = new JSONObject(result);
JSONObject listJson = new JSONObject(jsonParseData.getString("GetAllResult"));
if (listJson.getBoolean(getResources().getString(R.string.success))) {
**listParser.getListData(listJson);** // parser class parses data into ARRAYLIST
String[] ITEMS = new String[ListParser.listData.size()];
for (int i = 0; i < ITEMS.length; i++) {
ITEMS[i] = ListParser.listData.get(i).getName().toString();
mItemsID.add(ListParser.listData.get(i).getNameId().toString());
}
// Array to ArrayList
mItems = new ArrayList<String>(Arrays.asList(ITEMS));
mListSectionPos = new ArrayList<Integer>();
mListItems = new ArrayList<String>();
**synchronized (ListParser.listData) {
new Poplulate().execute(ListParser.listData);
// parser class uses data from ARRAYLIST... I think exception is throwing from here.
}**
} else {
CommonUtil.showValidationAlert(context, listJson.getString(getResources().getString(R.string.error)));
Log.i(TAG, " " + listJson.getString(getResources().getString(R.string.error)));
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
ToastUtil.ToastShort(context, "Server Timed out");
}
Populate AsyncTask Code goes from here:
private class Poplulate extends AsyncTask<ArrayList<listModel>, Void, Void> {
...
@Override
protected void onPreExecute() {
// show loading indicator
showLoading(mListView, mLoadingView, mEmptyView);
super.onPreExecute();
}
@Override
protected Void doInBackground(ArrayList<listModel>... params) {
mListItems.clear();
mListSectionPos.clear();
ArrayList<listModel> items = params[0];
if (mItems.size() > 0) {
// NOT forget to sort array
Collections.sort(ListParser.listData, new CustomComparator());
String prev_section = "";
synchronized (this) {
for (listModel current_item : items) {
String current_section = current_item.getName().substring(0, 1).toUpperCase(Locale.getDefault());
if (!prev_section.equals(current_section)) {
mListItems.add(current_section);
mListItems.add(current_item.getName());
// array list of section positions
mListSectionPos.add(mListItems.indexOf(current_section));
prev_section = current_section;
} else {
mListItems.add(current_item.getName());
}
}
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (!isCancelled()) {
if (mListItems.size() <= 0) {
showEmptyText(mListView, mLoadingView, mEmptyView);
} else {
setListAdaptor();
showContent(mListView, mLoadingView, mEmptyView);
}
}
super.onPostExecute(result);
}
}
Here is the stacktrace lines
03-24 18:23:24.681: E/AndroidRuntime(6841): FATAL EXCEPTION: AsyncTask #1
03-24 18:23:24.681: E/AndroidRuntime(6841): Process: com.smart.adlware, PID: 6841
03-24 18:23:24.681: E/AndroidRuntime(6841): java.lang.RuntimeException: An error occured while executing doInBackground()
03-24 18:23:24.681: E/AndroidRuntime(6841): at android.os.AsyncTask$3.done(AsyncTask.java:300)
03-24 18:23:24.681: E/AndroidRuntime(6841): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
03-24 18:23:24.681: E/AndroidRuntime(6841): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
03-24 18:23:24.681: E/AndroidRuntime(6841): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
03-24 18:23:24.681: E/AndroidRuntime(6841): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-24 18:23:24.681: E/AndroidRuntime(6841): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-24 18:23:24.681: E/AndroidRuntime(6841): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-24 18:23:24.681: E/AndroidRuntime(6841): at java.lang.Thread.run(Thread.java:841)
03-24 18:23:24.681: E/AndroidRuntime(6841): Caused by: java.util.ConcurrentModificationException
03-24 18:23:24.681: E/AndroidRuntime(6841): at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
03-24 18:23:24.681: E/AndroidRuntime(6841): at com.smart.adlware.CaregiverList$Populate.doInBackground(CaregiverList.java:324)
03-24 18:23:24.681: E/AndroidRuntime(6841): at com.smart.adlware.CaregiverList$Populate.doInBackground(CaregiverList.java:1)
03-24 18:23:24.681: E/AndroidRuntime(6841): at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-24 18:23:24.681: E/AndroidRuntime(6841): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-24 18:23:24.681: E/AndroidRuntime(6841): ... 4 more
So please assist me ASAP. I am stuck with this Exception.
synchronized (this) {
for (listModel current_item : items) {
String current_section = current_item.getName().substring(0, 1).toUpperCase(Locale.getDefault());
if (!prev_section.equals(current_section)) {
mListItems.add(current_section);
mListItems.add(current_item.getName());
// array list of section positions
mListSectionPos.add(mListItems.indexOf(current_section));
prev_section = current_section;
} else {
mListItems.add(current_item.getName());
}
}
}
The problem synchronized (this)
. Since you didn't provide stacktrace, I don't know the exception is thrown by which operation.
For example , if your exception is caused by multi-thread access to items , you can just replace this
with items
.
这篇关于Android的例外:ConcurentModificationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!