0

我是Android开发新手,并且正在努力解决这个一直给我带来一些挫折的问题。我正试图关闭这个进度Dialog。当我运行该应用程序时,它显示,信息被提取,并且UI被更新。但是,对话从来没有被驳回。ProgressDialog不会在线程中解雇

progDialog = ProgressDialog.show(HomeActivity.this, "", "Fetching info...", true, false); 
new Thread(new Runnable() { 
    public void run() { 
     fetchInfomation(userID); //fetches information - Works 
     runOnUiThread(new Runnable() { 
      public void run() { 
       setLayoutList(); //updates UI - Works 
       progDialog.dismiss(); //doesn't seem to close progress dialog 
       firstView(); //displays prompt - Works 
      } 
     }); 
     progDialog.dismiss(); //doesn't close dialog either 
    } 
}).start(); 

任何想法?

+0

阅读此:http://stackoverflow.com/q/891451/696723。你会从中得到一些想法 – CMA 2012-07-23 03:35:21

回答

0

您不能在外部线程内与UI进行交互。有一些技术可以做到这一点,但没有必要。您可以使用Handler

例如:

和:

.... 
new Thread() { 

    @Override 
    public void run() { 
      ... // do your background jobs here 
      mHandler.sendEmptyMessage(...); // interact with UI 
     }); 
    } 
}.start(); 
0

这将是很好的做法,如果你做的UI线程任何GUI更新。在任何线程内,你都可以运行你的UI线程,在那里你可以做UI的东西,否则你可以去消息处理程序也可以为你做同样的事情。

Runnable run_in_ui = new Runnable() { 
    @Override 
    public void run() { 
     // do your UI stuffs here 
    } 
}; 
runOnUiThread(run_in_ui);