2017-02-08 31 views
-3

我是Android開發新手。我試圖展示一個ProgressDialog。我看到很多教程,說爲顯示對話框必須使用線程。正如你可以看到snippet代碼正在使用線程。即使使用線程,ProgressDialog也不起作用

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     try { 
      refreshFromFeed(); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     setContentView(R.layout.activity_main); 
    } 

private void refreshFromFeed() throws InterruptedException { 
     ProgressDialog dialog = ProgressDialog.show(this,"Loading","Wake up after some sleep"); 
     Thread th = new Thread(){ 
      public void run(){ 
       Log.d("TimeFrom", String.valueOf(System.currentTimeMillis()/1000)); 
       try { 
        Thread.sleep(5000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 

       Log.d("TimeTo", String.valueOf(System.currentTimeMillis()/1000)); 
      } 
     }; 
     th.start(); 
     dialog.dismiss(); 
    } 
protected void onRefresh(View view) throws InterruptedException { 
     refreshFromFeed(); 
    } 

日誌顯示它花了5秒,但是,我看不到屏幕上有任何對話框,我可以在屏幕上做任何事情。即使我在物理設備上使用。我使用了調試模式。沒有例外。

onRefreshonClick一個事件,它的XML聲明。

回答

0

我對你的代碼做了一點改動,仔細閱讀。

private void refreshFromFeed() throws InterruptedException { 
    ProgressDialog dialog = ProgressDialog.show(this,"Loading","Wake up after some sleep"); 
    Thread th = new Thread(){ 
     public void run(){ 
      Log.d("TimeFrom", String.valueOf(System.currentTimeMillis()/1000)); 
      try { 
       Thread.sleep(5000); 
       dialog.dismiss(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

      Log.d("TimeTo",String.valueOf(System.currentTimeMillis()/1000)); 
     } 
    }; 
    th.start(); 

} 
+0

你說得對。我應該改變dismiss()方法。然而,它不工作:( –

+1

添加dialog.show();在威脅th –

+0

謝謝,它工作得很好 –

0

您在顯示它之後立即解散對話框。 也許你想移動你的「dialog.dismiss();」到線程內部。請記住,你需要解僱UI線程的對話框,否則會崩潰您的應用程序:

private void refreshFromFeed() throws InterruptedException { 
     final ProgressDialog dialog = ProgressDialog.show(this,"Loading","Wake up after some sleep"); 
     Thread th = new Thread(){ 
      public void run(){ 
       Log.d("TimeFrom", String.valueOf(System.currentTimeMillis()/1000)); 
       try { 
        Thread.sleep(5000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 

       Log.d("TimeTo", String.valueOf(System.currentTimeMillis()/1000)); 

       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         dialog.dismiss(); 
        } 
       }); 
      } 
     }; 
     th.start(); 
    } 

我看到很多教程,對於顯示必須使用線程對話框說。

你沒有明確需要一個線程來顯示ProgressDialog,這只是將其關閉的例子後5000毫秒

0
private void refreshFromFeed() throws InterruptedException { 
     final ProgressDialog dialog = ProgressDialog.show(getActivity(),"Loading","Wake up after some sleep"); 
     Thread th = new Thread() { 
      public void run() { 
       Log.d("TimeFrom", String.valueOf(System.currentTimeMillis()/1000)); 
       try { 
        Thread.sleep(5000); 
        dialog.dismiss(); // dismiss your dialog here 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 

       Log.d("TimeTo", String.valueOf(System.currentTimeMillis()/1000)); 
      } 
     }; 
     th.start(); 
    } 
+0

它沒有工作:( –

0

你仍然在你的UI線程中運行ProgressDialog。

ProgressDialog dialog = ProgressDialog.show(this,"Loading","Wake up after some sleep"); 

在此行之後創建的新線程,不在此行之前!

相關問題