2012-06-12 47 views
1

今天很簡單的問題。當我試圖檢索對話框上的按鈕時,我總是收到一個空值。帶按鈕的Android自定義對話框

@Override 
public void onRestart() { 
    super.onRestart(); 


    //must prompt with modal dialog for pin 
    Dialog dialog = new Dialog(this); 
    dialog.setOwnerActivity(this); 
    dialog.setCancelable(false); 

    //check pin 
    dialog.setCanceledOnTouchOutside(false); 
    //set view to enterpin XML screen 
    dialog.setContentView(R.layout.enterpin); 
    //register button 

    //show dialog 
    dialog.show(); 
    //listen for button being clicked 
    Button button = (Button) findViewById(R.id.pinlogin); 
    button.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v){ 
      EditText editText = (EditText) findViewById(R.id.enterpin); 
      int enteredPin = Integer.parseInt(editText.getText().toString()); 
      SharedPreferences sharedP = getPreferences(MODE_PRIVATE); 
      int temp = sharedP.getInt("pin", 0); 
      if(enteredPin==temp){ 
       pinCheck = true; 
      }else{ 
       pinCheck = false; 
      } 
     } 
    }); 
    if(pinCheck){ 
     dialog.dismiss(); 
    } 



} 

是的按鈕存在,沒有我沒有mispell它的參考。是的,我已經清理了這個項目並重新開啓了eclipse。當我清楚地調用setContentView()時,該按鈕如何不與視圖關聯?我相信這很簡單,我從來沒有使用過對話框,一般來說,新的GUI。尤其是Android的

回答

6
Button button = (Button)dialog.findViewById(R.id.pinlogin); 
         ^^^^^^ 

當你告訴對話框,你必須給refrences of dialog。因爲沒有它,Button refrences from main.xml。我從主視圖setContentView(R.layout.main);意思。