【问题标题】:IllegalStateException:The content of the adapter has changed but ListView did not receive a notificationIllegalStateException:适配器的内容发生了变化但ListView没有收到通知
【发布时间】:2015-05-01 04:14:44
【问题描述】:

直觉上,我知道,当它抛出 IllegalStateException 时。但就我而言,我无法猜测,源头的问题在哪里。我敢说,麻烦线就在那里:

private class LoadMoreDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        if (isCancelled()) {
            return null;
        }
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
        }
        OFFSET = OFFSET + COUNT;
        getWallsData();
        return null;
    }
    @Override
    protected void onPostExecute(Void result) { 

        mPostListAdapter.notifyDataSetChanged();                
        wallPostsList.onLoadMoreComplete();
        super.onPostExecute(result);
    }
    @Override
    protected void onCancelled() {
        wallPostsList.onLoadMoreComplete();
    }

有一条 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. [in ListView(2131099757, class com.costum.android.widget.LoadMoreListView) with Adapter(class android.widget.HeaderViewListAdapter)]

【问题讨论】:

    标签: android listview illegalstateexception


    【解决方案1】:

    问题已解决:找到答案 there ! 我做了什么?我添加了切换器 Boolean LOADMORE SWITCHER=false;作为全局可见性并替换为 mPostListAdapter.notifyDataSetChanged();从 doInBackground() 到 getWallsData()。

    private Boolean LOADMORE SWITCHER=false;
    ....
    private class LoadMoreDataTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            if (isCancelled()) {
                return null;
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            OFFSET = OFFSET + COUNT;
            LOADMORE=true;
            getWallsData();
            return null;
        }
        @Override
        protected void onPostExecute(Void result) { 
    
           // mPostListAdapter.notifyDataSetChanged(); was replaced to getWallsData() body
            wallPostsList.onLoadMoreComplete();
            super.onPostExecute(result);
        }
        @Override
        protected void onCancelled() {
            wallPostsList.onLoadMoreComplete();
        } 
    }
    .....
    getWallsData(){
         ...
         if(LOADMORE){
              mPostListAdapter.notifyDataSetChanged();
              LOADMORE=false;
    
    
         }
    }
    

    我非常非常高兴地说:我没有这个问题了:

    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. [in ListView(2131099757, class com.costum.android.widget.LoadMoreListView) with Adapter(class android.widget.HeaderViewListAdapter)] 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多