display progressdialog in non-activity class

后端 未结 3 2198
予麋鹿
予麋鹿 2021-01-26 07:36

I am trying to display a dialog in a non-Activity class. Basically, I detect an object in my app, I would like to display a dialog and then switch activities. I\'m getting a \"j

相关标签:
3条回答
  • 2021-01-26 08:14

    This is the exception you will get if you are performing any UI operation on any thread or from any background task. Also context.runOnUiThread won't work.

    Instead use:

    activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                 mDialog.dismiss();
                                }
                              });
    

    You can use the same for showing where activity is the object of activity.

    0 讨论(0)
  • 2021-01-26 08:23

    Your Dialog should be called from the UIthread so try to use this,

    context.this.runOnUiThread(new Runnable() {
    
                @Override
                public void run() {
                    mDialog.show();
    
                }
            });
    

    Hope this works.

    0 讨论(0)
  • 2021-01-26 08:25

    You do this

      context.runOnUIThread( new Runnable() {
                 public void run() {
                      // show the Dialog
                      mDialog.setTitle("Please wait");
                      mDialog.setMessage("Please wait");
    
                      mDialog.show();
                   }
                });
             }
    
         Thread.sleep(5000);
    
            context.runOnUIThread( new Runnable() {
                 public void run() {
                      // Dismiss the Dialog
                      mDialog.dismiss();
                   }
                });
    
    0 讨论(0)
提交回复
热议问题