11-06 19:52:25.958: E/AndroidRuntime(29609):
java.lang.IllegalStateException: 适配器的内容已更改,但ListView未收到通知。确保您的适配器的内容不是在后台线程中修改的,而只能在UI线程中修改。[在ListView(-1,类
android.widget.ListPopupWindow $ DropDownListView)中使用Adapter(class com.example.parkfoxxlight_android.PlacesAutoCompleteAdapter)]
完整日志:http://pastebin.com/Hx7k28Rm
适配器的完整代码:http://pastebin.com/TfH1bXE3我正在使用https://developers.google.com/places/training/autocomplete-android中的示例,它具有相当多的默认代码,因此似乎存在Google代码中的错误?
应用程序仅在出现以上错误消息时才会崩溃。
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。
mHandler.post
替换act.runOnUiThread
,并在构造函数中创建你的Handler。 - Tenfour04new PlacesAutoCompleteAdapter(getBaseContext())
来实例化您的适配器,当过滤器运行时,他们将会得到一个ClassCastException异常。 - Tenfour04Filter
的performFiltering()
方法在后台线程上运行,从该方法中,您正在更改适配器所基于的resultList
。如果您更改了该列表,并且在此期间ListView
访问了适配器,则它将看到某些内容已更改,但并不知道这一点(它将不会很高兴)。请检查此代码片段 https://gist.github.com/luksprog/7382576。 - user