接收错误java.lang.IllegalStateException:适配器的内容已更改,但ListView未收到通知

时间:2016-02-19 12:43:55

标签: android listview android-asynctask openerp onclicklistener

我发现了这个错误“确保未从后台线程修改适配器的内容确保适配器在内容发生变化时调用notifyDataSetChanged()。”有时候点击listview onClickListener

我不是每次都得到这个。我有时会随机收到此错误。但我不明白这个错误来自哪里。我正在使用Asynctask。

这是我的Asynctask代码,

new AsyncTask<String, Integer, Void>() {
    boolean error_server;

    protected void onPreExecute() {
        Asycdialog = new ProgressDialog(ReportingList.this);
        Asycdialog.setMessage("Loading...");
        Asycdialog.setCanceledOnTouchOutside(false);
        Asycdialog.show();
    };

    @Override
    protected Void doInBackground(String... arg0) {
        try {
            arraylist_product_category.clear();
            JSONArray jArray_cate = new JSONArray();
            jArray_cate.put("id");
            jArray_cate.put("name");

            JSONObject fieldsAccumulates_cate = new JSONObject();
            fieldsAccumulates_cate.put("fields", jArray_cate);
            JSONObject jo_cate = odoo.search_read("product.category", fieldsAccumulates_cate);

            JSONArray result_cate = jo_cate.getJSONArray("records");
            if (result_cate.length() > 0) {
                for (int i = 0; i < result_cate.length(); i++) {
                    JSONObject jo1 = result_cate.getJSONObject(i);
                    WorldPopulation wp = new WorldPopulation(jo1.getString("id"), jo1.getString("name"));
                    arraylist_product_category.add(wp);
                }
            } else {
                publishProgress(1);
            }

            //adapter_product_category = new ListViewAdapter(ReportingList.this, arraylist_product_category);
            //adapter_product_category.notifyDataSetChanged();
            is_adapter_product_category_filled = true;
        } catch (Exception e) {
            e.printStackTrace();
            error_server = true;
        }
        return null;
    };

    protected void onProgressUpdate(Integer... values) {
        if (values[0] == 1)
            et_product_category.setText("");
    };

    protected void onPostExecute(Void result) {
        adapter_product_category = new ListViewAdapter(ReportingList.this, arraylist_product_category);
        //is_adapter_product_category_filled = true;
        if(Asycdialog.isShowing())
            Asycdialog.dismiss();
        if (error_server)
            Toast.makeText(getApplicationContext(), "Server not reachable", Toast.LENGTH_LONG).show();
    };
}.execute(null, null, null);

请帮我解决此错误。我做错了什么?

这是我的打印堆栈跟踪,

02-19 19:03:53.299 184-1088/? E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 40
02-19 19:03:53.409 1175-1175/com.serpentcs.primemedical E/InputEventReceiver: Exception dispatching input event.
02-19 19:03:53.409 1175-1175/com.serpentcs.primemedical E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
02-19 19:03:53.419 1175-1175/com.serpentcs.primemedical E/MessageQueue-JNI: java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131493109, class android.widget.ListView) with Adapter(class com.serpentcs.primemedical.adapter.ListViewAdapter)]
    at android.widget.ListView.layoutChildren(ListView.java:1555)
    at android.widget.AbsListView.onTouchUp(AbsListView.java:3687)
    at android.widget.AbsListView.onTouchEvent(AbsListView.java:3485)
    at android.view.View.dispatchTouchEvent(View.java:7758)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2224)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2071)
    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1518)
    at android.app.Dialog.dispatchTouchEvent(Dialog.java:746)
    at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:59)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2019)
    at android.view.View.dispatchPointerEvent(View.java:7995)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4046)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3861)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3419)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3469)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3438)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3545)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3446)
    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3602)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3419)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3469)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3438)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3446)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3419)
    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5694)
        at android.view.ViewRootIm
02-19 19:03:53.419 1175-1175/com.serpentcs.primemedical D/AndroidRuntime: Shutting down VM
02-19 19:03:53.419 1175-1175/com.serpentcs.primemedical W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x430f3140)
02-19 19:03:53.419 1175-1175/com.serpentcs.primemedical E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.serpentcs.primemedical, PID: 1175
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131493109, class android.widget.ListView) with Adapter(class com.serpentcs.primemedical.adapter.ListViewAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1555)
at android.widget.AbsListView.onTouchUp(AbsListView.java:3687)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3485)
at android.view.View.dispatchTouchEvent(View.java:7758)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2224)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2230)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1973)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2071)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1518)
at android.app.Dialog.dispatchTouchEvent(Dialog.java:746)
at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:59)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2019)
at android.view.View.dispatchPointerEvent(View.java:7995)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4046)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3861)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3419)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3469)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3438)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3545)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3446)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3602)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3419)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3469)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3438)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3446)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3419)
at android.view.ViewRootImpl.de
02-19 19:03:53.429 534-954/? W/ActivityManager:   Force finishing activity com.serpentcs.primemedical/.ReportingList

添加adapter_product_category.notifyDataSetChanged();后,这是我更新的代码,

new AsyncTask<String, Integer, Void>() {
                    boolean error_server;

                    protected void onPreExecute() {
                        Asycdialog = new ProgressDialog(ReportingList.this);
                        Asycdialog.setMessage("Loading...");
                        Asycdialog.setCanceledOnTouchOutside(false);
                        Asycdialog.show();
                    };

                    @Override
                    protected Void doInBackground(String... arg0) {

                        try {
                            arraylist_product_category.clear();
                            JSONArray jArray_cate = new JSONArray();
                            jArray_cate.put("id");
                            jArray_cate.put("name");

                            JSONObject fieldsAccumulates_cate = new JSONObject();
                            fieldsAccumulates_cate.put("fields", jArray_cate);
                            JSONObject jo_cate = odoo.search_read("product.category", fieldsAccumulates_cate);

                            JSONArray result_cate = jo_cate.getJSONArray("records");
                            if (result_cate.length() > 0) {
                                for (int i = 0; i < result_cate.length(); i++) {
                                    JSONObject jo1 = result_cate.getJSONObject(i);
                                    WorldPopulation wp = new WorldPopulation(jo1.getString("id"), jo1.getString("name"));
                                    arraylist_product_category.add(wp);
                                }
                            } else {
                                publishProgress(1);
                            }

                            adapter_product_category = new ListViewAdapter(ReportingList.this, arraylist_product_category);
                            adapter_product_category.notifyDataSetChanged();
                            is_adapter_product_category_filled = true;
                        } catch (Exception e) {
                            e.printStackTrace();
                            error_server = true;
                        }
                        return null;
                    };

                    protected void onProgressUpdate(Integer... values) {
                        if (values[0] == 1)
                            et_product_category.setText("");
                    };

                    protected void onPostExecute(Void result) {
                        //adapter_product_category = new ListViewAdapter(ReportingList.this, arraylist_product_category);
                        adapter_product_category.notifyDataSetChanged();
                        //is_adapter_product_category_filled = true;
                        if(Asycdialog.isShowing())
                            Asycdialog.dismiss();
                        if (error_server)
                            Toast.makeText(getApplicationContext(), "Server not reachable", Toast.LENGTH_LONG).show();

                    }

                    ;
                }.execute(null, null, null);

1 个答案:

答案 0 :(得分:1)

在类 -

中全局声明适配器
ListViewAdapter adapter_product_category = new    ListViewAdapter(ReportingList.this, arraylist_product_category);


new AsyncTask<String, Integer, Void>() {
                boolean error_server;

                protected void onPreExecute() {
                    Asycdialog = new ProgressDialog(ReportingList.this);
                    Asycdialog.setMessage("Loading...");
                    Asycdialog.setCanceledOnTouchOutside(false);
                    Asycdialog.show();
                };

                @Override
                protected Void doInBackground(String... arg0) {

                    try {
                        arraylist_product_category.clear();
                        JSONArray jArray_cate = new JSONArray();
                        jArray_cate.put("id");
                        jArray_cate.put("name");

                        JSONObject fieldsAccumulates_cate = new JSONObject();
                        fieldsAccumulates_cate.put("fields", jArray_cate);
                        JSONObject jo_cate = odoo.search_read("product.category", fieldsAccumulates_cate);

                        JSONArray result_cate = jo_cate.getJSONArray("records");
                        if (result_cate.length() > 0) {
                            for (int i = 0; i < result_cate.length(); i++) {
                                JSONObject jo1 = result_cate.getJSONObject(i);
                                WorldPopulation wp = new WorldPopulation(jo1.getString("id"), jo1.getString("name"));
                                arraylist_product_category.add(wp);
                            }
                        } else {
                            publishProgress(1);
                        }

                        //adapter_product_category = new ListViewAdapter(ReportingList.this, arraylist_product_category);
                        //adapter_product_category.notifyDataSetChanged();
                        is_adapter_product_category_filled = true;
                    } catch (Exception e) {
                        e.printStackTrace();
                        error_server = true;
                    }
                    return null;
                };

                protected void onProgressUpdate(Integer... values) {
                    if (values[0] == 1)
                        et_product_category.setText("");
                };

                protected void onPostExecute(Void result) {
                    // Set notifyDataSetChanged for updates
                    adapter.notifyDataSetChanged();

                    //is_adapter_product_category_filled = true;
                    if(Asycdialog.isShowing())
                        Asycdialog.dismiss();
                    if (error_server)
                        Toast.makeText(getApplicationContext(), "Server not reachable", Toast.LENGTH_LONG).show();

                }

                ;
            }.execute(null, null, null);