gpt4 book ai didi

java - AsyncTask 中的 ProgressDialog 未关闭

转载 作者:行者123 更新时间:2023-12-01 12:18:52 24 4
gpt4 key购买 nike

我有一个 AsyncTask 问题。我有一个带有三个复选框的 Activity ,如果选中,则当用户按下按钮时启动异步任务。我的异步是这样的

    private class MyTask extends AsyncTask<Void, Void, Void> {

String valore

public MyTask(String valore) {
this.valore = valore;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage(getString(R.string.message));
progressDialog.setIndeterminate(true);
progressDialog.show();
}

@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
// Exec some operations

return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if(risultato != null) {
textView.append(risultato);
}
if(errori != null) {
textView.append(errori);
}
progressDialog.dismiss();
}

}

还有按钮

button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(checkBox.isChecked()) {
new MyTask("string").execute();
}
if(checkBox2.isChecked()) {
new MyTask("string2").execute();
}
if(checkBox3.isChecked()) {
new MyTask("string3").execute();
}
}
});

问题是,如果选中两个或三个复选框,则 ProgressDialog 不会消失并保留在屏幕上。为什么?当选中两个或多个复选框时,如何将其关闭?

最佳答案

 try {
if ((pDialog != null) && pDialog.isShowing()) {
pDialog.dismiss();
}
} catch (final IllegalArgumentException e) {
// Handle or log or ignore
} catch (final Exception e) {
// Handle or log or ignore
} finally {

pDialog = null;
}

Try dismissing the dialog like this It might solve your problem

这段代码有问题

 public void onClick(View v) {
// TODO Auto-generated method stub
if(checkBox.isChecked()) {
new MyTask("string").execute();
}
if(checkBox2.isChecked()) {
new MyTask("string2").execute();
}
if(checkBox3.isChecked()) {
new MyTask("string3").execute();
}
}

this is besically a logical error if we dry run this code it will execute as many time as number of checked checkbox increases . Suppose you do the checkbox1 to checked it will execute the async 1 time while when you click checkbox2 it will execute checkbox1 async as well as checkbox2 async and it be so on so change the condition on the button onclicklistner

关于java - AsyncTask 中的 ProgressDialog 未关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26825353/

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