我一直在运行android 5的三星手机上遇到java.lang.IllegalStateException异常。它只发生在运行android 5(API 21)的三星手机上。
我的Logcat说明如下:
Fatal Exception: 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(-1,
class android.widget.ListPopupWindow$DropDownListView) with
Adapter(classcom.commandsoftware.androidbookingapp.Activity.Fragments.PlacesAPIFragment.AutoCompleteSearch$GooglePlacesAutocompleteAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1582)
at android.widget.AbsListView.onLayout(AbsListView.java:2632)
at android.view.View.layout(View.java:16550)
at android.view.ViewGroup.layout(ViewGroup.java:5303)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:16550)
at android.view.ViewGroup.layout(ViewGroup.java:5303)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2308)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2021)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1180)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6558)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
我的适配器代码如下所示:
class GooglePlacesAutocompleteAdapter extends ArrayAdapter implements Filterable {
private ArrayList resultList;
public GooglePlacesAutocompleteAdapter(Context context) {
super(context, R.layout.autocomplete_list_item);
}
@Override
public int getCount() {
if (resultList != null) {
return resultList.size();
} else {
return 0;
}
}
@Override
public Object getItem(int index) {
return resultList.get(index);
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
resultList = autocomplete(constraint.toString());
filterResults.values = resultList;
if (resultList != null) {
filterResults.count = resultList.size();
} else {
filterResults.count = 0;
}
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
try {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
}
}
任何帮助都会很棒。我已经在SO上尝试过最受欢迎的答案,但没有一个能帮上忙。
我的直觉告诉我,这与这些行有关
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}