简体   繁体   English

ProgressDialog无法停止

[英]ProgressDialog cant stop

my app is loading some data from a web server.this time i would like to have a progress dialog in order to avoid a black screen to the app users. 我的应用程序正在从Web服务器加载一些数据。这次,我希望有一个进度对话框,以避免对应用程序用户造成黑屏。

 Button ok = (Button) findViewById(R.id.ok);
        ok.setOnClickListener(new View.OnClickListener() {
              public void onClick(View view) {
                 MyDialog = ProgressDialog.show( yassou.this, " " , " Loading. Please wait ... ", true);
                    MyDialog.show();
               // myIntent.username.getText();
                  try {

                      Send();

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                  Status();
                  //MyDialog=null;
              } 

          });

this is the btn witch "onclick" is loading the data. 这是btn女巫“ onclick”正在加载数据。 Send() ; Send() ; is a method that sends some data that the user enters to my server,and Status(); 是一种将用户输入的数据和Status();发送到我的服务器的方法Status(); is a second method that directs me to a new page.unfortunately,as i press the ok button,the app first goes to the second page and secondly appears a non stop progress dialog.Where is my wrong please? 是将我定向到新页面的第二种方法。不幸的是,当我按“确定”按钮时,该应用程序首先进入第二页,然后出现一个不间断的进度对话框。请问我哪里错了?

You have to dismiss dialogs when you don't need them anymore. 当您不再需要对话框时,必须将其关闭。 It appears after you are sent to the "second page" because all the onClick() code is executed before changes to the UI happen. 它在您发送到“第二页”之后显示,因为所有onClick()代码都是在对UI进行更改之前执行的。

It's important that you do all the Send() and Status() work in an AsyncTask or in a thread (which, at the end, will dismiss the dialog), because while that code is being executed the UI will be locked, and won't be able to show the dialog. AsyncTask或线程(最后将关闭对话框)中完成所有Send()Status()工作非常重要,因为在执行该代码时,UI将会被锁定并赢得无法显示对话框。 Moreover, if the job is too long you could get an Application Not Responding (ANR) message. 此外,如果作业时间太长,您可能会收到“应用程序无响应”(ANR)消息。

A good rule is that any method executed in UI thread should last less than 200ms, to guarantee responsiveness and not to result sluggish to the user. 一个好的规则是,在UI线程中执行的任何方法都应持续200ms以内,以确保响应速度,并且不会导致用户呆滞。

Creating Dialogs (from Android Dev) 创建对话框 (来自Android Dev)

try this 尝试这个

 Button ok = (Button) findViewById(R.id.ok);
    ok.setOnClickListener(new View.OnClickListener() {
          public void onClick(View view) {
             MyDialog = ProgressDialog.show( yassou.this, " " , " Loading. Please wait ... ", true);
                MyDialog.show();
           // myIntent.username.getText();
              try {

                  Send();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            MyDialog.dismiss();
              Status();
              //MyDialog=null;
          } 

      });

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

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