我试图在AsyncTask的onPostExecute()中删除对话框,并在textview中设置文本,但是textview没有改变,我得到了#34; Window已经聚焦,忽略焦点获得..."。
以下是代码:
protected void onPreExecute() {
dialog = ProgressDialog.show(mContext, "","Loading...", true);
}
protected void onPostExecute(Object result) {
dialog.dismiss();
tv.setText("some text");
}
显示进度对话框,当我的后台工作完成后,它将被取消,但textview没有变化。没有进度对话框,文本视图会更新。
任何想法,解决这个问题?
谢谢!
答案 0 :(得分:2)
在 dialog.dismiss()
之后 尝试此代码段 。
if (!dialog.isShowing())
{
TextView tv = (TextView) findViewById(R.id.textView);
tv.setText("some text");
}
答案 1 :(得分:0)
public class MyActivity extends Activity
{
/**
* Called when the activity is first created.
*/
ProgressBar progressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progressBar = (ProgressBar) findViewById(R.id.pb);
TextView view=(TextView) findViewById(R.id.myt);
new Task(view).execute();
}
通过在构造函数中传递TextView实例,我们可以在进度条中使用
private class Task extends AsyncTask<Void, Integer, Void> {
TextView view;
private Task(TextView textView) {
this.view= textView;
}
int myProgress;
@Override
protected void onPreExecute() {
myProgress = 0;
super.onPreExecute();
}
@Override
protected void onPostExecute(Void aVoid) {
Toast.makeText(MyActivity.this,
"onPostExecute", Toast.LENGTH_LONG).show();
super.onPostExecute(aVoid);
}
@Override
protected void onProgressUpdate(Integer... values) {
progressBar.setProgress(values[0]);
view.setText("Value"+myProgress);
super.onProgressUpdate(values);
}
@Override
protected void onCancelled() {
super.onCancelled(); //To change body of overridden methods use File | Settings | File Templates.
}
@Override
protected Void doInBackground(Void... voids) {
while (myProgress < 100) {
myProgress++;
publishProgress(myProgress);
SystemClock.sleep(100);
}
return null;
}
}
}