tre*_*159 9 android exception android-asynctask
我尝试修改AsyncTaks中的Spinner内容,但我不能和Logcat写的"09-19 16:36:11.189:ERROR/ERROR THE(6078):只有创建视图层次结构的原始线程可以触及它观点."
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);*/
//}
}
}
}
Run Code Online (Sandbox Code Playgroud)
函数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;
}
}
Run Code Online (Sandbox Code Playgroud)
怎么了?谢谢.
eri*_*icn 27
如Peter所述,您无法使用访问视图doInBackground().你可以在里面做到这一点onPostExecute().doInBackground()据我所知,这就是你应该使用结果返回的地方.
我遇到了这个问题并通过移动视图修改代码来修复它onPostExecute().
对于那些刚刚开始接受Android开发的人:
- doInBackground():不同于另一个线程(在后台)发生的内容与您的views/fragment/activity操作的主/原始线程不同(这是AsyncTask== 的全部内容)>你不能触摸视图!
- onPostExecute():现在背景的东西都完成了,这里它回到主/线程上==>你现在可以触摸视图了!