2011-04-27 64 views
0

我想檢查一個條件,然後如果它是假的,我想關閉先前顯示的AlertDialog。 不過,我面臨着這樣的錯誤:錯誤與alertdialog.dismiss()

的方法解僱()是未定義的類型AlertDialog.Builder

代碼:

ad.show(); 
     if (call.isInCall()== false) 
     { 
      ad.dismiss(); 
     } 

問題是什麼?

編輯:問題:

AlertDialog.Builder ad = new AlertDialog.Builder(context); 
     d = ad.create(); 
     ad.setTitle("Appel en cours..."); 
     ad.setMessage("Voulez vous répondre à cet appel?"); 
     //ad.create(); 
     ad.setPositiveButton("Oui", 
..... 

if(call.isInCall() == false && d != null && d.isShowing()){ 
      d.dismiss(); 
     } 

=>強制關閉。 非常感謝您的幫助。

回答

4

您必須先使用該構建器才能創建對話框,然後才能執行此類操作。

//Let's change this so you have a field declared in your class. 
AlertDialog d; 

//Somewhere, maybe in onCreate() you're using the builder to instantiate the dialog. 

//insert all builder creation and methods here first... then call 
d = ad.create(); 

//somewhere else in your code you've shown the dialog with 
d.show(); 

//again, some where else you're checking if the dialog is displaying and dismissing it 
if(call.isInCall() == false && d != null && d.isShowing()){ 
    d.dismiss(); 
} 

你必須根據你在哪裏調用此代碼要小心,當然,你在AlertDialog範圍。這也不是處理對話框的推薦方法。你應該研究使用onCreateDialog()onPrepareDialog()Activity回調:http://developer.android.com/guide/topics/ui/dialogs.html

+0

我會加AlertDialog d = ad.create();在顯示AlertDialog(ad.show())之後?我添加了它,錯誤仍然存​​在。 – androniennn 2011-04-27 14:21:41

+0

@androniennn不,你不應該從AlertDialog.Builder中調用show()。我會添加一個編輯... – Maximus 2011-04-27 14:25:55

+0

請看我做了什麼(第一篇文章),我有一個力量關閉。感謝你們對我的幫助。 – androniennn 2011-04-27 14:37:22