Error while calling AsyncTask within a gesture listener <p>Right now, I have an Activity that displays 10 listings from a <code>JSON</code> array. Next, on the swipe of an <code>ImageView</code>, I want to clear the <code>ListView</code> and display the next 10 (as a "next page" type thing). So, right now I do this</p> <pre><code> view.setOnTouchListener(new OnSwipeTouchListener(getBaseContext()) { @Override public void onSwipeLeft() { //clear adapter adapter.clear(); //get listings 10-20 startLoop = 10; endLoop = 20; //call asynctask to display locations FillLocations myFill = new FillLocations(); myFill.execute(); Toast.makeText(getApplicationContext(), "Left", Toast.LENGTH_LONG).show(); } </code></pre> <p>and when I swipe it diisplays ONE item and I get this error <code>Error Parsing Data android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.</code> What am I doing wrong? Thanks!</p> <p>Full code:</p> <pre><code>public class MainActivity extends ActionBarActivity { ListView listView; int startLoop, endLoop; TextView test; ArrayList&lt;Location&gt; arrayOfLocations; LocationAdapter adapter; ImageView view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startLoop = 0; endLoop = 10; listView = (ListView) findViewById(R.id.listView1); // Construct the data source arrayOfLocations = new ArrayList&lt;Location&gt;(); // Create the adapter to convert the array to views adapter = new LocationAdapter(this, arrayOfLocations); FillLocations myFill = new FillLocations(); myFill.execute(); view = (ImageView) findViewById(R.id.imageView1); view.setOnTouchListener(new OnSwipeTouchListener(getBaseContext()) { @Override public void onSwipeLeft() { adapter.clear(); startLoop = 10; endLoop = 20; FillLocations myFill = new FillLocations(); myFill.execute(); Toast.makeText(getApplicationContext(), "Left", Toast.LENGTH_LONG).show(); } }); } private class FillLocations extends AsyncTask&lt;Integer, Void, String&gt; { String msg = "Done"; protected void onPreExecute() { progress.show(); } // Decode image in background. @Override protected String doInBackground(Integer... params) { String result = ""; InputStream isr = null; try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://americanfarmstands.com/places/"); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); isr = entity.getContent(); // resultView.setText("connected"); } catch (Exception e) { Log.e("log_tag", "Error in http connection " + e.toString()); } // convert response to string try { BufferedReader reader = new BufferedReader( new InputStreamReader(isr, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } isr.close(); result = sb.toString(); } catch (Exception e) { Log.e("log_tag", "Error converting result " + e.toString()); } // parse json data try { JSONArray jArray = new JSONArray(result); for (int i = startLoop; i &lt; endLoop; i++) { //Toast.makeText(getApplicationContext(), i, // Toast.LENGTH_LONG).show(); final JSONObject json = jArray.getJSONObject(i); // counter++; String initialURL = "http://afs.spotcontent.com/img/Places/Icons/"; final String updatedURL = initialURL + json.getInt("ID") + ".jpg"; Bitmap bitmap2 = null; try { bitmap2 = BitmapFactory .decodeStream((InputStream) new URL(updatedURL) .getContent()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { adapter.add(new Location(bitmap2, json .getString("PlaceTitle"), json .getString("PlaceDetails"), json .getString("PlaceDistance"), json .getString("PlaceUpdatedTime"))); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } catch (Exception e) { // TODO: handle exception Log.e("log_tag", "Error Parsing Data " + e.toString()); } return msg; } protected void onPostExecute(String msg) { // Attach the adapter to a ListView //ListView listView = (ListView) findViewById(R.id.listView1); listView.setAdapter(adapter); progress.dismiss(); } } </code></pre> <p>Location Adapter:</p> <pre><code>public class LocationAdapter extends ArrayAdapter&lt;Location&gt; { public LocationAdapter(Context context, ArrayList&lt;Location&gt; locations) { super(context, R.layout.item_location, locations); } @Override public View getView(int position, View convertView, ViewGroup parent) { // Get the data item for this position Location location = getItem(position); // Check if an existing view is being reused, otherwise inflate the view if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_location, parent, false); } // Lookup view for data population TextView tvName = (TextView) convertView.findViewById(R.id.tvName); TextView tvDetails = (TextView) convertView.findViewById(R.id.tvDetails); TextView tvDistance = (TextView) convertView.findViewById(R.id.tvDistance); TextView tvHours = (TextView) convertView.findViewById(R.id.tvHours); ImageView ivIcon = (ImageView) convertView.findViewById(R.id.imgIcon); // Populate the data into the template view using the data object tvName.setText(location.name); tvDetails.setText(location.details); tvDistance.setText(location.distance); tvHours.setText(location.hours); ivIcon.setImageBitmap(location.icon); // Return the completed view to render on screen return convertView; } } </code></pre> <p>EDIT: Updated Code:</p> <p>for (int i = startLoop; i &lt; endLoop; i++) {</p> <pre><code> // Toast.makeText(getApplicationContext(), i, // Toast.LENGTH_LONG).show(); final JSONObject json = jArray.getJSONObject(i); // counter++; String initialURL = "http://afs.spotcontent.com/img/Places/Icons/"; final String updatedURL = initialURL + json.getInt("ID") + ".jpg"; final Bitmap bitmap2 =BitmapFactory .decodeStream((InputStream) new URL(updatedURL) .getContent()); //try { // bitmap2 = BitmapFactory // .decodeStream((InputStream) new URL(updatedURL) // .getContent()); //} catch (MalformedURLException e) { // e.printStackTrace(); //} catch (IOException e) { // e.printStackTrace(); //} MainActivity.this.runOnUiThread(new Runnable() { @Override public void run() { try { adapter.add(new Location(bitmap2, json .getString("PlaceTitle"), json .getString("PlaceDetails"), json .getString("PlaceDistance"), json .getString("PlaceUpdatedTime"))); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } </code></pre>