gpt4 book ai didi

android异步任务edittext

转载 作者:行者123 更新时间:2023-11-29 16:17:18 24 4
gpt4 key购买 nike

我有一段代码查询我的网络服务器 xml 解析返回的数据并用相关数据填充我的 GUI 上的文本字段。在我的 oncreate 函数中有这个之前,代码工作正常。但是我想向用户显示一个加载对话框,所以我将 web 服务器和 xml 解析操作移到了一个异步任务中。现在当我用我的解析数据填充我的 GUI 文本字段时,问题就出现了,我得到了一个错误。谁能看出我做错了什么

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()

您必须将查询结果保存在类对象中才能执行此操作

关于android异步任务edittext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8583359/

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