2016-02-19 15 views
0

我收到此錯誤「確保適配器的內容未從後臺線程修改請確保您的適配器調用notifyDataSetChanged()當其內容改變時。「有時點擊列表視圖onClickListener接收錯誤java.lang.IllegalStateException:適配器的內容已更改,但ListView未收到通知

我沒有得到這個每一次。我有時會隨機得到這個錯誤。但我不明白這個錯誤來自哪裏。我正在使用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); 
+0

當你改變listview的數據時總是調用listview中的notifyDataSetChanged() –

+0

Ca你提供Printstackstrace? – Ajinkya

+0

您缺少'adapter_product_category.notifyDataSetChanged()'。在'doInBackground()'和'onPostExecute()'地方。 – Shark

回答

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); 
相關問題