![](/img/trans.png)
[英]Array Adapter NotifyDataSetChanged() NullPointerException Android
[英]Error using notifyDataSetChanged in android array adapter
11-06 19:52:25.958:E / AndroidRuntime(29609):java.lang.IllegalStateException:適配器的內容已更改,但ListView未收到通知。 確保不從后台線程修改適配器的內容,而只是從UI線程修改。 [在ListView(-1,類android.widget.ListPopupWindow $ DropDownListView)中使用Adapter(類com.example.parkfoxxlight_android.PlacesAutoCompleteAdapter)]
完整日志: http : //pastebin.com/Hx7k28Rm
適配器的完整代碼: http : //pastebin.com/TfH1bXE3我使用的是https://developers.google.com/places/training/autocomplete-android中的示例,它有相當的默認代碼,因此它似乎有一個谷歌代碼中的錯誤?
應用程序有時會崩潰,但上面的錯誤消息。
protected void publishResults(CharSequence constraint,
FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
活動http://pastebin.com/FYzYtvXY :
public class CityActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.city);
AutoCompleteTextView autoCompView = (AutoCompleteTextView) findViewById(R.id.autocomplete_city);
PlacesAutoCompleteAdapter ad = new PlacesAutoCompleteAdapter(this);
ProgressBar b = (ProgressBar)findViewById(R.id.progressBar1);
ad.setLoadingIndicator(b);
autoCompView.setAdapter(ad);
}
}
任何想法如何解決這一問題? 我在android 4.3上。
Filter
的performFiltering()
方法在后台線程上運行,並且從該方法更改適配器所基於的resultList
。 如果你更改了那個數據列表,那么ListView
訪問適配器就會看到一些東西在沒有它知情的情況下發生了變化(並且不會很高興)。 您應該避免在performFiltering
方法中使用resultList
,只需創建一個新的臨時列表:
// in the performFiltering method which runs on a background thread:
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
ArrayList<String> queryResults;
if (constraint != null && constraint.length() > 0) {
queryResults = autocomplete(constraint);
} else {
queryResults = new ArrayList<String>(); // empty list/no suggestions showing if there's no valid constraint
}
filterResults.values = queryResults;
filterResults.count = queryResults.size();
return filterResults; // ## Heading ##
}
private List<String> autocomplete(String input) {
// don't use the here the resultList List on which the adapter is based!
// some custom code to get items from http connection
ArrayList<String> queryResults = new ArrayList<String>(); // new list
queryResults.add("Some String");
return queryResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
// update the data with the new set of suggestions
resultList = (ArrayList<String>)results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
試試這個(只是一個猜測):
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
ArrayList list = autocomplete(constraint.toString());
if (list != null) {
filterResults.values = list;
filterResults.count = list.size();
}
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
//change the underlying data immediately before notifying UI
resultList = (ArrayList)results.values;
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}};
return filter;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.