Android例外:ConcurentModificationException

时间:2015-03-23 11:12:12

标签: java android listview arraylist

在我们选择的项目详情的背面加载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);
    }
}

这是stacktrace行

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

所以请尽快帮助我。我坚持这个例外。

1 个答案:

答案 0 :(得分:1)

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

问题synchronized (this)由于您没有提供堆栈跟踪,我不知道该操作会引发异常。

例如,如果您的例外是由对项目的多线程访问引起的,那么您只需将this替换为items