Note that there are some explanatory texts on larger screens.

plurals
  1. POjava.lang.IllegalStateException - why??? (Android, ListAdapter)
    primarykey
    data
    text
    <p>Time to time users get following exception (I myself can't reproduce it):</p> <pre><code>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(2131165209, class android.widget.ListView) with Adapter(class com.tfd.mobile.TfdSearch.TfdSearch$SearchList$PagesListAdapter)] at android.widget.ListView.layoutChildren(ListView.java:1567) at android.widget.AbsListView.onTouchModeChanged(AbsListView.java:2093) at android.view.ViewTreeObserver.dispatchOnTouchModeChanged(ViewTreeObserver..java:591) at android.view.ViewRoot.ensureTouchModeLocally(ViewRoot.java:2023) at android.view.ViewRoot.ensureTouchMode(ViewRoot.java:2007) at android.view.ViewRoot.handleMessage(ViewRoot.java:1775) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4633) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) at dalvik.system.NativeStart.main(Native Method) </code></pre> <p>I can't find why it happened. I use threading, but all underlying data changes in main thread. And I'm always call _adapter.notifyDataSetChanged(); after data has been changed. Please help me! This is my code:</p> <pre><code> private class SearchList extends LinkedList&lt;PageInfo&gt; { 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() &gt;= 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 &amp;&amp; typedText.length() &gt; 0) getSuggestions(typedText); // if no recent - use suggestions else _adapter.notifyDataSetChanged(); } } public class PagesListAdapter extends ArrayAdapter&lt;PageInfo&gt; { private LayoutInflater mInflater; public PagesListAdapter(Context context, List&lt;PageInfo&gt; 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; } } }; </code></pre> <p>The only place where data is changing - when "refresh" method calls. It always calls from main thread. Why it still happen??? Help!</p> <p><strong>WHEN EXACTLY java.lang.IllegalStateException EXCEPTION OCCURED? ** **When I expressly run "refresh" method from non-UI thread, I get another exception (CalledFromWrongThreadException) at moment of _adapter.notifyDataSetChanged() call:</strong></p> <pre><code>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) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload