java.lang.IllegalStateException-为什么???(Android、ListAdapter)
有时用户会遇到以下异常(我自己无法复制): 我不明白为什么会这样。我使用线程,但所有底层数据都在主线程中更改。我总是调用_adapter.notifyDataSetChanged();数据更改后。请帮帮我!这是我的代码:java.lang.IllegalStateException-为什么???(Android、ListAdapter),java,android,listview,illegalstateexception,Java,Android,Listview,Illegalstateexception,有时用户会遇到以下异常(我自己无法复制): 我不明白为什么会这样。我使用线程,但所有底层数据都在主线程中更改。我总是调用_adapter.notifyDataSetChanged();数据更改后。请帮帮我!这是我的代码: private class SearchList extends LinkedList<PageInfo> { private static final long serialVersionUID = 1L; private PagesLi
private class SearchList extends LinkedList<PageInfo>
{
private static final long serialVersionUID = 1L;
private PagesListAdapter _adapter;
public PagesListAdapter getAdapter()
{
return _adapter;
}
public SearchList(ListView listView)
{
super();
_adapter = new PagesListAdapter(activity, this);
listView.setAdapter(_adapter);
}
private void getSuggestions(final String typedText)
{
showProgressVisibility(true);
new Thread(new Runnable() {
public void run() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
final String[] sugg = TfdMode.getMode().getSuggestions(typedText);
activity.runOnUiThread(new Runnable() {
public void run() {
if (sugg != null)
{
clear();
for (String s: sugg) {
add(new PageInfo(s, PageInfo.PAGE_SUGGESTION, null));
}
}
_adapter.notifyDataSetChanged();
showProgressVisibility(false);
}
});
}
}).start();
}
public void refresh(final String typedText)
{
if (typedText.length() >= 3)
{
// use suggestions (separate thread)
getSuggestions(typedText);
}
else
{
// use recent
clear();
for (PageInfo p: TfdMode.getMode().recentManager.pages) {
if (p.text.startsWith(typedText)) add(p);
}
if (size() == 0 && typedText.length() > 0)
getSuggestions(typedText); // if no recent - use suggestions
else
_adapter.notifyDataSetChanged();
}
}
public class PagesListAdapter extends ArrayAdapter<PageInfo> {
private LayoutInflater mInflater;
public PagesListAdapter(Context context, List<PageInfo> objects) {
super(context, R.layout.bookmarks_item, R.id.bookmark_title, objects);
mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final PageInfo b = getItem(position);
View row = convertView == null ? mInflater.inflate(R.layout.bookmarks_item, null) : convertView;
TextView title = (TextView) row.findViewById(R.id.bookmark_title);
ImageView img = (ImageView) row.findViewById(R.id.bookmark_img);
ImageView del = (ImageView) row.findViewById(R.id.bookmark_img_del);
del.setVisibility(View.GONE);
title.setText(b.text);
int iconId = b.getIconResId();
if (iconId != -1)
{
img.setImageResource(iconId);
img.setVisibility(View.VISIBLE);
}
else
img.setVisibility(View.INVISIBLE);
return row;
}
}
};
您必须从UI线程调用refresh,该线程可能不是主线程。您可以使用它在UI线程上运行某些内容:
Activity.runOnUiThread(Runnable)
->
\u适配器。notifyDataSetChanged()代码>
这必须在UI线程上执行。使用处理程序或runOnUiThread从何处调用刷新?我从以下位置调用刷新:1。editText的TextChangeListener。2.setDisplayMode方法。这个方法从代码周围的不同地方调用,但是在调用刷新之前,这个方法中涉及了很多UI对象,所以如果从非UI线程调用它,在“刷新”得到调用机会之前,它会失败得更多。
10-12 13:20:29.057: ERROR/AndroidRuntime(24746): FATAL EXCEPTION: Thread-27
10-12 13:20:29.057: ERROR/AndroidRuntime(24746): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
10-12 13:20:29.057: ERROR/AndroidRuntime(24746): at android.view.ViewRoot.checkThread(ViewRoot.java:2837)
10-12 13:20:29.057: ERROR/AndroidRuntime(24746): at android.view.ViewRoot.invalidateChild(ViewRoot.java:619)
10-12 13:20:29.057: ERROR/AndroidRuntime(24746): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:645)
10-12 13:20:29.057: ERROR/AndroidRuntime(24746): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)
10-12 13:20:29.057: ERROR/AndroidRuntime(24746): at android.view.View.invalidate(View.java:5192)
10-12 13:20:29.057: ERROR/AndroidRuntime(24746): at android.view.View.setFlags(View.java:4569)
10-12 13:20:29.057: ERROR/AndroidRuntime(24746): at android.view.View.setVisibility(View.java:3083)
10-12 13:20:29.057: ERROR/AndroidRuntime(24746): at com.tfd.mobile.TfdSearch.TfdSearch.showProgressVisibility(TfdSearch.java:315)
10-12 13:20:29.057: ERROR/AndroidRuntime(24746): at com.tfd.mobile.TfdSearch.TfdSearch.access$0(TfdSearch.java:313)
10-12 13:20:29.057: ERROR/AndroidRuntime(24746): at com.tfd.mobile.TfdSearch.TfdSearch$SearchList.getSuggestions(TfdSearch.java:783)
10-12 13:20:29.057: ERROR/AndroidRuntime(24746): at com.tfd.mobile.TfdSearch.TfdSearch$SearchList.refresh(TfdSearch.java:823)
10-12 13:20:29.057: ERROR/AndroidRuntime(24746): at com.tfd.mobile.TfdSearch.TfdSearch$8$1.run(TfdSearch.java:443)
10-12 13:20:29.057: ERROR/AndroidRuntime(24746): at java.lang.Thread.run(Thread.java:1096)
Activity.runOnUiThread(Runnable)