2013-04-13 59 views
0

我使用此方法showDatePickerDialog(this.getCurrentFocus());加載對話框。這很好。但是當我在setOnClickListener裏面顯示按鈕單擊對話框時,出現編譯器錯誤。錯誤是The method getCurrentFocus() is undefined for the type new View.OnClickListener(){}。我的代碼遵循showDatePickerDialog無法加載DatePicker對話框

frombutton.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      showDatePickerDialog(this.getCurrentFocus()); 

     } 
    }); 

回答

2

的問題是,即new OnClickListener()創建內部類,這是不Activity類型的,如外部類。

因此,this反映到OnClickListener,而不是你的活動。方法getCurrentFocus() - 方法不存在於偵聽器類中,因此無法找到。

解決該問題的方法是明確指出您希望this來自周邊(外部)類。你這樣做,通過添加外的類名前this

showDatePickerDialog(YOURACTIVITYCLASSNAMEHERE.this.getCurrentFocus()); 
+0

盧卡斯克努特是正確的,應使用活動情境。 – Raghunandan