android asynctask edittext
我有一段代码查询我的web服务器xml解析返回的数据并在我的GUi中填充相关数据的文本域。之前我有我的oncreate函数中的这个代码工作正常。不过,我想向用户显示一个加载对话框,所以我将Web服务器和xml解析操作移到了asynctask中。现在的问题是,当我用我的解析数据填充我的GUI文本字段时,我得到了一个错误。任何人都可以看到我在做什么错android asynctask edittext
new BackgroundAsyncTask().execute(); /// called from the oncreate function
,我的后台任务的代码如下
public class BackgroundAsyncTask extends
AsyncTask<Void, Integer, Void> {
int myProgress;
@Override
protected void onPostExecute(Void result) {
MyDialog.dismiss();
}
@Override
protected void onPreExecute() {
MyDialog = ProgressDialog.show(attraction_more_info.this, " " , " Loading. Please wait ... ", true);
}
@Override
protected Void doInBackground(Void... params) {
xml query and parse stuff on here ...
// Populate page now
TextView titlefield = (TextView) findViewById(R.id.att_title);
TextView add1field = (TextView) findViewById(R.id.att_address1);
TextView add2field = (TextView) findViewById(R.id.att_address2);
TextView townfield = (TextView) findViewById(R.id.att_town);
TextView postcodefield = (TextView) findViewById(R.id.att_postcode);
TextView phonefield = (TextView) findViewById(R.id.att_phone);
WebView webview = (WebView) findViewById(R.id.webview1);
MY ERRORS START HERE
titlefield.setText(attraction_name);
add1field.setText(attraction_address1);
add2field.setText(attraction_address2);
townfield.setText(attraction_town);
postcodefield.setText(attraction_postcode);
phonefield.setText(attraction_phone);
webview.loadData(attraction_description, "text/html", null);
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
}
}
任何人都可以帮我吗?
您无法从非UI线程更新UI元素。尝试移动所有的setText()调用和webview.loadData()来onPostExecute()
你必须保存在类对象的查询结果做
但是我们已经读过,我们可以从异步task.UI更新更新UI是不可能在线程中。 – Sameer 2011-12-21 04:30:41
@TofeeqAhmad您可以通过doInBackground()以外的方法从异步任务更新UI(或从doInBackground()中调用)doInBackground在单独的线程中运行。 – antlersoft 2011-12-21 17:43:36
试试这个,
add1field.post(new Runnable() {
public void run() {
add1field.setText(attraction_address1);
}
});
还没有读取您的代码,但错误是什么,它发生在哪里?请提供日志。 – 2011-12-20 23:39:04