只有创建视图层次结构的原始线程才能触及其视图

问题描述 投票:0回答:2

我在一些Activity中添加了进度对话框。但是我在title.how中提到Exception以解决它。

dialog = ProgressDialog.show(Notification.this, "loading please wait",                
"Loading. Please wait...", true);

new Thread() {
 public void run() {
   try{
     performBackgroundProcess1();
     //sleep(3000,000);
   } catch (Exception e) {
     Log.e("tag", e.getMessage());
 }

 // dismiss the progress dialog
     dialog.dismiss();
  }
}.start();

this.all后面的任何问题都是在performbackgroundprocess方法中执行的。

android progressdialog
2个回答
0
投票

你不能调用dialog.dismiss();在后台线程中。您可以使线程在处理完成后向处理程序发送消息,并且在处理程序中您可以关闭对话框。处理程序在ui线程中工作

关于它有一个tutorial


0
投票

使用runOnUiThread:

        new Thread() {
     public void run() {
       try{
         performBackgroundProcess1();
         //sleep(3000,000);
       } catch (Exception e) {
         Log.e("tag", e.getMessage());
     }

// dismiss the progress dialog
      CurrentActivity.this.runOnUiThread(new Runnable(){  
    @Override  
    public void run() {  
    // TODO Auto-generated method stub  
    dialog.dismiss();  
    }  
    });    
      }
    }.start();
© www.soinside.com 2019 - 2024. All rights reserved.