繁体   English   中英

从Activity调用时,另一个AsyncTask类中的ProgressDialog不显示

[英]ProgressDialog in another AsyncTask class not showing when called from Activity

我有一个名为RestClient的类,该类从我的webService中获取一些信息,然后返回,而我正在尝试使Progress对话框在访问Internet时运行。 而且,由于我在多个地方使用了此类,所以我不会在Activity本身中做任何事情。 这是我的RestClient类:

public class RestClient extends AsyncTask<URL, String, String> {

    private Context context;
    private String string;
public RestClient(Context context, String string)
{
    this.context = context;
    this.string = string;
}

@Override
protected void onPreExecute() {
    dialog = ProgressDialog.show(context, "Buscando seu Produto","Por favor, espere um momento...",true ,false);
    //I've already tried:
    /*ProgressDialog dialog = new ProgressDialog(context);
    dialog.setTitle("Buscando seu Produto");
    dialog.setMessage("Por favor, espere um momento...");
    dialog.setIndeterminate(true);
    dialog.setCancelable(false);*/

    dialog.show();
    super.onPreExecute();
}

@Override
protected String doInBackground(URL... params) {
    try {

        //Some WebService gets and Json conversions using my string variable
        //and some Thread.sleep that counts 2000 miliseconds to do all the queries

        dialog.dismiss();

    } catch (IOException | InterruptedException |JSONException e) {
        e.printStackTrace();

        dialog.dismiss();

        return e.getMessage();
    }
    return null;
}

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);


   }
}

在我的活动中,当我单击如下按钮时,我将调用RestClient类:

---编辑:我忘了提到我在同一活动中有一个AlertDialog,有时可以在ProgressDialog之前和之后显示-

private Button buttonConfirm;
     private EditView evString;

     private String theString;
     private String returnFromExecute;

     private RestClient restClient;


     private AlertDialog.Builder dialog;

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_access_webservice);

            evString = (EditText) findViewById(R.id.editViewMyString);
            buttonConfirm = (Button) findViewById(R.id.buttonConfirm);

            dialog = new ProgressDialog(IdentificacaoDeProdutoActivity.this);
            dialog.setTitle("Error");
            dialog.setMessage("Please try again");
            dialog.setIndeterminate(true);
            dialog.setCancelable(false);


            buttonConfirmar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)  {                  
                    theString = evString.getText().toString();
                    if(!(theString!=null && theString.trim().length()>0)) //To check if theString is not null
                    {
                          dialog.show();
                    }

                    restClient = new RestClient(AccessWebserviceActivity.this, theString);

                    //Then I call execute and put a Thread.sleep a bit longer to compensate the ones I have in my doInBackground
                    restClient.execute();
                    try {
                        Thread.sleep(2050);

                    } catch (Exception e) {
                        dialog.show();
                        return;
                    }
                }
            }
        }

问题是我的ProgressDialog从不显示。 我已经尝试过getParent()getApplication()getApplicationContext()而不是AccessWebserviceActivity.this但是都没有用。 有人知道发生了什么,该怎么办?

您尚未创建进度对话框,请尝试此操作。

ProgressDialog dialog;

 @Override
    protected void onPreExecute() {
        dialog= new ProgressDialog(context);
        dialog.setMessage("on Progress");
        dialog.show();
        super.onPreExecute();
    }

你需要打电话

dialog = ProgressDialog.show(context, "Buscando seu Produto","Por favor, espere um momento...",true ,false);

并删除

 dialog.show();

还把你的dialog.dismiss() ; onPostExecute ()中的方法。 这个dialog.dismiss()方法在catch块中很好,但是如果在try块中调用此方法,它的目的是什么。 一旦调用此AsyncTask ,它将删除进度对话框。

returnFromExecute = restClient.get();

删除该语句。 您已经:

restClient.execute();

那应该做。

结果doInBackground()你应该在处理onPostExecute() 无法在onCreate()处理或检索它。

经过大量关于线程和进程的研究,我发现必须将RestClient.execute之后的所有代码封装在一个

new Thread(new Runnable() { public void run() { // My code } });

这样代码的执行就在后台以及WebService查询中进行。

编辑:

即使创建新线程可行,也不建议这样做! 正确的做法是创建另一个扩展AsyncTask来完成工作的类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM