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

时间:2015-07-24 10:21:31

标签: android listview android-fragments

我有一个视图寻呼机。在视图内部寻呼机有3个片段,每个片段包含一个lisview。当我从一个片段滑动到另一个片段时,有时会遇到IllegalStateException。

logcat的

 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(2131427495, class android.widget.ListView) with Adapter(class com.neona.helperclasses.HistoryRecycleListAdapter)]
        at android.widget.ListView.layoutChildren(ListView.java:1562)
        at android.widget.AbsListView.onLayout(AbsListView.java:2148)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1626)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
        at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1692)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
        at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1692)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:907)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.java:4966)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
        at android.view.View.layout(View.java:15596)
        at android.view.ViewGroup.layout(ViewGroup.

Listview代码

 public void Load(){

    new AsyncTask<String, String, String>(){

        protected void onPreExecute() {

            historyitems = new ArrayList<Historyitem>();
            historyitems.clear();
            historyadapter = new HistoryRecycleListAdapter(getActivity(),
                    getActivity(),historyitems);
            hisrecyclerview.setAdapter(historyadapter);


        };
        @Override
        protected String doInBackground(String... arg0) {

            try{
                final List<NameValuePair> list=new ArrayList<NameValuePair>();
                list.add(new BasicNameValuePair("id", "user"));

                final HttpParams httpParams = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
                DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
                HttpPost httpPost = new HttpPost(url);
                httpPost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
                try {
                    httpPost.setEntity(new UrlEncodedFormEntity(list));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    HttpResponse response = httpClient.execute(httpPost);

                    BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                    String json = reader.readLine();


                    JSONObject jsonObj = new JSONObject(json);
                    if(jsonObj.has("control")){

                        JSONArray feedArray = jsonObj.getJSONArray("control");
                        for (int i = 0; i < feedArray.length(); i++) {
                            JSONObject feedObj = (JSONObject) feedArray.get(i);

                            final Historyitem item = new Historyitem();

                            if(feedObj.has("Patient_Attribute")){
                                if(!feedObj.getString("attribute").equals("")){
                                    item.setP_attribute(feedObj.getString("attribute"));

                                }
                            }

                            historyitems.add(item);
                        }
                    }else{
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch(NullPointerException e){
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            } catch (Exception e){
            }
            return null;
        }
        protected void onPostExecute(String result) {
            System.out.println(historyitems.size());


                        hisrecyclerview.addFooterView(btnLoadMore);
                        hisrecyclerview.setAdapter(historyadapter);                        
                    }                   
        };
    }.execute();
}

3 个答案:

答案 0 :(得分:1)

UI操作应该在onPostExecute中完成。您正尝试在onPreExecute上设置listview的适配器。您的AsyncTask应该是这样的:

new AsyncTask<String, String, String>(){

    protected void onPreExecute() {

        historyitems = new ArrayList<Historyitem>();
        historyitems.clear();

    };
    @Override
    protected String doInBackground(String... arg0) {

        try{
            final List<NameValuePair> list=new ArrayList<NameValuePair>();
            list.add(new BasicNameValuePair("id", "user"));

            final HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
            DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
            HttpPost httpPost = new HttpPost(url);
            httpPost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(list));
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                HttpResponse response = httpClient.execute(httpPost);

                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                String json = reader.readLine();


                JSONObject jsonObj = new JSONObject(json);
                if(jsonObj.has("control")){

                    JSONArray feedArray = jsonObj.getJSONArray("control");
                    for (int i = 0; i < feedArray.length(); i++) {
                        JSONObject feedObj = (JSONObject) feedArray.get(i);

                        final Historyitem item = new Historyitem();

                        if(feedObj.has("Patient_Attribute")){
                            if(!feedObj.getString("attribute").equals("")){
                                item.setP_attribute(feedObj.getString("attribute"));

                            }
                        }

                        historyitems.add(item);
                    }
                }else{
                }
            } catch (JSONException e) {
                e.printStackTrace();
            } catch(NullPointerException e){
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();

            }
        } catch (Exception e){
        }
        return null;
    }
    protected void onPostExecute(String result) {
        System.out.println(historyitems.size());

        historyadapter = new HistoryRecycleListAdapter(getActivity(), getActivity(),historyitems);
        hisrecyclerview.setAdapter(historyadapter);

        hisrecyclerview.addFooterView(btnLoadMore);
        hisrecyclerview.setAdapter(historyadapter);                        
    }                   
};
}.execute();

设置listview适配器的代码将移至onPostExecute

祝你好运。

答案 1 :(得分:0)

替换这个:

o.Add();

使用:

Order(Order o)

在你的asynk任务后执行方法

并在将适配器添加到列表之前添加页脚

protected void onPostExecute(String result) {
        System.out.println(historyitems.size());


                    hisrecyclerview.addFooterView(btnLoadMore);
                    hisrecyclerview.setAdapter(historyadapter);                        
                }                   
    };
}.execute();

答案 2 :(得分:0)

尝试在onPostExecute方法中执行此操作:

 getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    hisrecyclerview.addFooterView(btnLoadMore);
                    hisrecyclerview.setAdapter(historyadapter);
                    historyadapter.notifyDataSetChanged(); 
                }
            });}

它对我有用

当你使用AsyncTask处理runOnUiThread中的所有UI操作时