2011-02-01 36 views
2

我想包括ProgressDialog在我的應用程序。但它沒有出現。ProgressDialog沒有出現在活動

此處,我使用ProgressDialog代碼片段:

public class abcActivity extends Activity { 
    public boolean onOptionsItemSelected(MenuItem item) { 
     case XYZ: 
      ProgressDialog dialog = ProgressDialog.show(abcActivity.this, "", "Please wait for few seconds...", true); 
      callSomeFunction(); 
      dialog.dismiss(); 
      showToast(getString(R.string.SomeString)); 
      break; 
    } 
} 

有誰知道爲什麼對話框不顯示出來?任何線索?

+0

也許它沒有進入那個case語句?嘗試添加一個`Log.v(「onOptionsItemSelected」,「in XYZ」);` - 看看它是否到達那裏。 – xil3 2011-02-01 11:04:27

+0

xil3是正確的,它不能輸入`XYZ`。 或者也許callSomeFunction()正在執行快? – gnclmorais 2011-02-01 11:10:17

+0

打印callSomeFunction()中的日誌。這意味着開關盒被擊中。 – webgenius 2011-02-01 11:39:27

回答

9

我覺得你的代碼是錯誤的,你做的所有在UI線程的感覺。你必須把callsomefunction()放到後臺線程中。

public void runSomething() 
{ 
    showDialog(BACKGROUND_ID); 
    Thread t = new Thread(new Runnable() 
    {     
     public void run() 
     { 
      //do something 
      handler.post(finishThread); 
     } 
    }); 

    t.start(); 
    // The progress wheel will only show up once all code coming here has been executed 
} 

而且還有

protected Dialog onCreateDialog(int id) 
{ 
    if(progressDialog == null) progressDialog = new ProgressDialog(this); 
    return progressDialog; 
} 

@Override 
protected void onPrepareDialog(int id, Dialog dialog) 
{ 
    if(id == BACKGROUND_ID) 
    { 
     progressDialog.setIndeterminate(true); 
     progressDialog.setCancelable(false); 
     progressDialog.setMessage("running for long ..."); 
    } 
} 

Runnable finishThread = new Runnable() 
{  
    public void run() 
    { 
//long running 
     if(progressDialog != null) progressDialog.dismiss(); 
    } 
}; 
0

確保你有這樣的代碼通過調試或添加日誌調用。該代碼似乎對我來說。另外,如果您想在後臺執行某些操作並在執行時顯示進度對話框,請使用帶ProgressDialog的AsyncTask有界,如here

0

我認爲問題是你的開關狀態請覈實。

這裏採用的是Android顯示對話框試試這個另一種方法。

public static final int DIALOG2_KEY = 1; 
public static ProgressDialog dialog1; 

showDialog(DIALOG2_KEY); // use it where you want to display dialog 

dialog1.cancel(); // use it to cancel the dialog 


@Override 
    public Dialog onCreateDialog(int id) { 
     if (id == 1) { 
      dialog1 = new ProgressDialog(this); 
      dialog1.setMessage("Please wait..."); 
      dialog1.setIndeterminate(true); 
      dialog1.setCancelable(true); 
     } 
     return dialog1; 
    } 
0

ProgressDialog採用的是Android O,使用進度過時了。