gpt4 book ai didi

java - 安卓异常: ConcurentModificationException

转载 作者:行者123 更新时间:2023-12-01 11:45:09 25 4
gpt4 key购买 nike

当 ListView 从我们选择的项目的详细信息中加载后退时,我不一致地收到此异常。

当我进入这个 ListView 页面时,它永远不会抛出异常,但是当我进入这个 ListView 项目并删除/更新 ListView 时。

这是我的代码

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");
}

填充 AsyncTask 代码来自此处:

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);
}
}

这是堆栈跟踪行

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

所以请尽快帮助我。我被这个异常困住了。

最佳答案

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());
}
}
}

问题已同步(此)由于您没有提供stacktrace,所以我不知道异常是由哪个操作引发的。

例如,如果您的异常是由多线程访问 items 引起的,则只需将 this 替换为 items 即可。

关于java - 安卓异常: ConcurentModificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29208804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com