http://stackoverflow.com/questions/7474379/asynctask-only-the-original-thread-that-created-a-view-hierarchy-can-touch-its



879d9e1805a6495a9e3cbeeffe6da2ad.pngi.gif?e=eyJhdiI6NDE0LCJhdCI6NCwiY20iOjI5MTEsImNoIjoxMTc4LCJjciI6NTkxNywiZGkiOiIxMDJlNTRiOGU1ZDg0NWM5ODZmZTgwNDg5ZTczNjk0ZSIsImRtIjoxLCJmYyI6MTAxODIsImZsIjo3NDcyLCJrdyI6ImFuZHJvaWQsZXhjZXB0aW9uLGFuZHJvaWQtYXN5bmN0YXNrIiwibnciOjIyLCJwYyI6MCwicHIiOjE2MDQsInJ0IjoxLCJyZiI6Imh0dHBzOi8vd3d3Lmdvb2dsZS5jby5rci8iLCJzdCI6ODI3Nywiem4iOjQzLCJ0cyI6MTM5NTc3MDI2NTAxMX0&s=mP3hSXMwcjlwNjJRGKyz7ebFdME

I try to modify the Spinner content across the AsyncTaks but I can't and the Logcat is wrote "09-19 16:36:11.189: ERROR/ERROR THE(6078): Only the original thread that created a view hierarchy can touch its views.".

public class GetGroups extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        Spinner combo = (Spinner) findViewById(R.id.group_combo);

        setGroups(combo);

        return null;
    }

    @Override
    protected void onPostExecute(Void unused)
    {
        super.onPostExecute(unused);


        Spinner combo = (Spinner) findViewById(R.id.severity_combo);

        combo.setSelection(1);

        //updateGroups();
        //if (!isFinishing())
        //{
            /*Spinner combo = (Spinner) findViewById(R.id.group_combo);
            ProgressBar pg = (ProgressBar) findViewById(R.id.loading_group);

            pg.setVisibility(ProgressBar.GONE);
            combo.setVisibility(Spinner.VISIBLE);

            combo.setSelection(0);*/
        //}
    }
}

}

And the function setGroups is:

 public void setGroups(Spinner combo) {

    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(this.object.url);

        List<NameValuePair> parameters = new ArrayList<NameValuePair>(2);
        parameters.add(new BasicNameValuePair("user", this.object.user));
        parameters.add(new BasicNameValuePair("pass", this.object.password));
        parameters.add(new BasicNameValuePair("op", "get"));
        parameters.add(new BasicNameValuePair("op2", "groups"));
        parameters.add(new BasicNameValuePair("other_mode", "url_encode_separator_|"));
        parameters.add(new BasicNameValuePair("return_type", "csv"));
        parameters.add(new BasicNameValuePair("other", ";"));

        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters);

        httpPost.setEntity(entity);

        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity entityResponse = response.getEntity();

        String return_api = this.object.convertStreamToString(entityResponse.getContent());

        String[] lines = return_api.split("\n");

        ArrayList<String> array = new ArrayList<String>();

        for (int i= 0; i < lines.length; i++) {
            String[] groups = lines[i].split(";", 21);

            this.pandoraGroups.put(new Integer(groups[0]), groups[1]);

            array.add(groups[1]);
        }

        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item,
            array);
        combo.setAdapter(spinnerArrayAdapter);
    }
    catch (Exception e) {
        Log.e("ERROR THE ", e.getMessage());

        return;
    }
}

What is wrong? Thanks.

share|improve this question
   
is GetGroups an inner class? –  Tariq Patel Sep 19 '11 at 16:46
1 
What are you trying to do? –  userSeven7s Sep 19 '11 at 16:51
   
   
No @IceMAN sir, that should be a different approach. Even if such is related to AsyncTask usage, that would be too confusing for there person seeking help here. What do you think about my answer below? –  fuzzybeeMay 2 '13 at 4:38
add comment

You are trying to access UI components (View) from a background thread in your case inside the doInBackground() method. You are not allowed to do that.

share|improve this answer
add comment
a61df59863ed44e59446d0b9bb805c56.jpgi.gif?e=eyJhdiI6NDE0LCJhdCI6NCwiY20iOjg0NywiY2giOjExNzgsImNyIjoxMDc2OCwiZGkiOiI2NGY0MzhkYzlmMjg0NTRjOTY3M2M1OWVhZTg1N2ZkYSIsImRtIjoxLCJmYyI6MTY4ODUsImZsIjoyNDQ0LCJrdyI6ImFuZHJvaWQsZXhjZXB0aW9uLGFuZHJvaWQtYXN5bmN0YXNrIiwibnciOjIyLCJwYyI6MCwicHIiOjE1NjgsInJ0IjoxLCJyZiI6Imh0dHBzOi8vd3d3Lmdvb2dsZS5jby5rci8iLCJzdCI6ODI3Nywiem4iOjQ0LCJ0cyI6MTM5NTc3MDI2NTAxM30&s=T2u9FvidWy07Y6mJbSPnR7xseFk

As mentioned by Peter, you cannot access the views using doInBackground(). You can do that insideonPostExecute() however. That is where you are supposed to work with the results doInBackground() return as far as I know.

I ran into this issue and fixed it by moving the view modification code to onPostExecute().

share|improve