Note that there are some explanatory texts on larger screens.

plurals
  1. POHandler will not bind to main thread
    primarykey
    data
    text
    <p>So my code seems to run just fine until it hits this line</p> <pre><code>adapter.notifyDataSetChanged(); </code></pre> <p>The error that pops up in the logcat is CalledFromWrongThreadException. The debug also shows the handler being run in the Background thread. How do I get the handler to bind to the main thread, and not the background one? I thought I just had to create the handler in the main thread, but I guess I am wrong, quite possible I am new to andriod. How do I fix this?</p> <pre><code>//Imports are included public class DirectoryActivity extends ListActivity { private ProgressDialog ProgressDialog = null; private ArrayList&lt;DirectoryListing&gt; listing = null; private DirectoryAdapter adapter; private Runnable viewOrders; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.directory); final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (listing != null &amp;&amp; listing.size() &gt; 0) { adapter.notifyDataSetChanged(); for (int i = 0; i &lt; listing.size(); i++) adapter.add(listing.get(i)); Log.e("log_tag", "\nStill running\n"); } ProgressDialog.dismiss(); adapter.notifyDataSetChanged(); } }; listing = new ArrayList&lt;DirectoryListing&gt;(); adapter = new DirectoryAdapter(this, R.layout.rows, listing); setListAdapter(adapter); ProgressDialog = ProgressDialog.show(DirectoryActivity.this, "Please wait...", "Retrieving data ...", true); viewOrders = new Runnable() { @Override public void run() { listing = PreparePage.getArrayList(); handler.handleMessage(null); } }; Thread thread = new Thread(null, viewOrders, "Background"); thread.start(); } private static class PreparePage { protected static ArrayList&lt;DirectoryListing&gt; getArrayList() { ArrayList&lt;DirectoryListing&gt; listings = new ArrayList&lt;DirectoryListing&gt;(); JSONObject information = GetPageData.getJSONFromURL(url); Iterator key = information.keys(); while (key.hasNext()) { String id = (String) key.next(); JSONObject info = null; try { info = information.getJSONObject(id); } catch (JSONException e) { e.printStackTrace(); } String name = "", title = "", photo = ""; try { name = info.get("firstName") + " " + info.get("lastName"); title = info.getJSONObject("position").getString("name"); photo = info.optString("photoPath", "none"); } catch (JSONException e) { e.printStackTrace(); } listings.add(new DirectoryListing(name, title, photo)); } return listings; } } </code></pre> <p>}</p>
    singulars
    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.
    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