ProgressDialog无法停止

发布于 2024-10-13 09:29:08 字数 897 浏览 8 评论 0原文

我的应用程序正在从网络服务器加载一些数据。这次我想要一个进度对话框,以避免应用程序用户出现黑屏。

 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;
              } 

          });

这是“onclick”正在加载数据的 btn。Send();是一种将用户输入的一些数据发送到我的服务器的方法,Status(); 是第二种方法,它将我定向到新页面。不幸的是,当我按下“确定”按钮时,应用程序首先进入第二页,然后出现一个不停的进度对话框。请问我错在哪里?

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.

 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.Send(); is a method that sends some data that the user enters to my server,andStatus(); 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我太投入 2024-10-20 09:29:08

当您不再需要对话框时,您必须关闭它们。它出现在您被发送到“第二页”之后,因为所有 onClick() 代码都是在 UI 发生更改之前执行的。

AsyncTask 或线程中完成所有 Send()Status() 工作(最后,将关闭对话框),因为在执行该代码时,UI 将被锁定,并且无法显示对话框。此外,如果作业太长,您可能会收到应用程序未响应 (ANR) 消息。

一个好的规则是,在 UI 线程中执行的任何方法的持续时间都应少于 200 毫秒,以保证响应能力,并且不会导致用户反应迟缓。

创建对话框(来自 Android Dev)

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.

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. Moreover, if the job is too long you could get an Application Not Responding (ANR) message.

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.

Creating Dialogs (from Android Dev)

顾挽 2024-10-20 09:29:08

试试这个

 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;
          } 

      });

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;
          } 

      });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文