AsyncTask的"只有创建视图层次可以触摸其观点和QUOT原来的线程; [英] AsyncTask "Only the original thread that created a view hierarchy can touch its views."

查看:116
本文介绍了AsyncTask的"只有创建视图层次可以触摸其观点和QUOT原来的线程;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试修改整个AsyncTaks的微调内容,但我不能及的logcat是写下了09-19 16:36:11.189:错误/错误的(6078):只有原始的创建一个视图层次的线程可以触摸自己的看法。

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);*/
        //}
    }
}
}

和功能setGroups是:

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.

推荐答案

正如彼得,你不能使用访问视图 doInBackground()。你可以()但是这样做在 onPostExecute。这就是你应该对结果的工作 doInBackground()返回据我所知。

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

我遇到了这个问题,并通过移动视图修改code到 onPostExecute()

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

这篇关于AsyncTask的&QUOT;只有创建视图层次可以触摸其观点和QUOT原来的线程;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆