2013-10-18 34 views
1

我正在製作一個使用TimePicker的應用程序,這是我第一次使用它。我的代碼如下:使用Android TimePicker.getCurrentHour的問題

final Dialog mTimeDialog = new Dialog(this); 
TimePicker timePicker = (TimePicker)mTimeDialog.findViewById(R.id.timePicker); 
TimePicker timePicker1 = (TimePicker)mTimeDialog.findViewById(R.id.timePicker1); 
int starthour = timePicker.getCurrentHour(); 
int startminute = timePicker.getCurrentMinute(); 
int finishhour = timePicker1.getCurrentHour(); 
int finishminute = timePicker1.getCurrentMinute(); 

但我收到一個異常,當我嘗試運行它。當我按保存鍵觸發onClick,這是包含在,我得到這個:

Caused by: java.lang.NullPointerException 
at com.csam.hourtracker.hourentry.onClick(hourentry.java:40) 

任何幫助將是巨大的!

+0

什麼是40行'hourentry.java'? – Raghunandan

+0

第40行是int starthour = timePicker.getCurrentHour();對不起,應該已經指定了。 – dacelbot

+0

你的'timePicker'爲空 – Raghunandan

回答

2

你有以下

final Dialog mTimeDialog = new Dialog(this); 
TimePicker timePicker = (TimePicker)mTimeDialog.findViewById(R.id.timePicker); 

您還沒有初始化之前設置的佈局,以對話的內容TimerPicker的

你缺少

mTimeDialog.setContentView(yourview); 

因此您timepicker的是空造成NPE。

findViewById尋找一個視圖,其中包含在當前的對象佈局中提到的id。我相信你有對話框的自定義佈局。您需要首先膨脹佈局,然後初始化您的視圖。

+0

謝謝!只需添加setContentView即可解決該問題。我現在有其他問題......但希望我能解決它們。 – dacelbot

+0

@dacelbot發佈一個包含相關細節的新問題。如果我能,我一定會幫助 – Raghunandan