IllegalStateException:The 适配器的内容已更改但 ListView 未收到通知
IllegalStateException:The content of the adapter has changed but ListView did not receive a notification
凭直觉,我知道,当它抛出 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)]
问题是 fixed:the 找到答案 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)]
凭直觉,我知道,当它抛出 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)]
问题是 fixed:the 找到答案 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)]