gpt4 book ai didi

android - 异步任务 "Only the original thread that created a view hierarchy can touch its views."

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:16:19 27 4
gpt4 key购买 nike

我尝试跨 AsyncTaks 修改 Spinner 内容,但我不能,Logcat 写为“09-19 16:36:11.189: ERROR/ERROR THE(6078): 只有创建 View 层次结构的原始线程可以触摸它的 View 。”。

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是:

 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;
}
}

怎么了?谢谢。

最佳答案

正如 Peter 所提到的,您无法使用 doInBackground() 访问 View 。但是,您可以在 onPostExecute() 中执行此操作。据我所知,这就是您应该处理 doInBackground() 返回结果的地方。

我遇到了这个问题,并通过将 View 修改代码移动到 onPostExecute() 来修复它。

致那些刚开始接触 Android 开发的人:
- doInBackground():与您的 View /fragment/Activity 运行的主线程/原始线程不同的另一个线程(在后台)发生的任何内部情况(这是 AsyncTask< 的重点 ==> 你不能触摸 View !
- onPostExecute():现在后台工作已经完成,这里它回到了主/线程线程 ==> 你现在可以触摸 View 了!

关于android - 异步任务 "Only the original thread that created a view hierarchy can touch its views.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7474379/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com