2013-06-04 50 views
1

我正在使用Robotium來測試我的項目。在測試時,我打電話給datePickerDialog.getDatePicker()我總是得到一個NullPointerExcpetion。NullPointerException在調用DatePickerDialog.getDatePicker()

我DatePickerDialogFragment代碼:

public class DatePicker_Fragment extends SherlockDialogFragment{ 
    public static final int START_DATE = 1; 
    public static final int END_DATE = 2; 
    public static final String TAG = "DatePicker"; 
    private OnDateSetListener listener_m; 

    public DatePicker_Fragment(){ 

    } 

    @SuppressLint("NewApi") 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) {  

     //get the current time 
     Calendar calendar = Calendar.getInstance(); 
     int year = calendar.get(Calendar.YEAR); 
     int month = calendar.get(Calendar.MONTH); 
     int day = calendar.get(Calendar.DAY_OF_MONTH); 

     //create the dialog 
     DatePickerDialog datePickerDialog = new DatePickerDialog(getSherlockActivity(),listener_m,year, month, day); 
     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){ 
      //datePickerDialog.getDatePicker().setSpinnersShown(false); 
      //datePickerDialog.getDatePicker().setCalendarViewShown(true); 
     } 

     return datePickerDialog; 
    } 

    public void setListener(OnDateSetListener listener){ 
     listener_m = listener; 
    } 

} 

這是從我的測試

//Select the start date 
TextView startDate = (TextView) solo.getView(R.id.tv_StartDate); 
solo.clickOnView(startDate); 

//wait for dialog fragment 
solo.waitForFragmentByTag(DatePicker_Fragment.TAG); 


//set the start date 
SherlockFragmentActivity current_activity = (SherlockFragmentActivity) solo.getCurrentActivity(); 
DatePicker_Fragment dp_frag = (DatePicker_Fragment) current_activity.getSupportFragmentManager().findFragmentByTag(DatePicker_Fragment.TAG); 
DatePickerDialog dp_dialog = (DatePickerDialog) dp_frag.getDialog(); 
DatePicker datePicker = dp_dialog.getDatePicker(); 
solo.setDatePicker(datePicker,2013,6,21); 
solo.clickOnText("Set"); 

相關的代碼,我總是在呼籲dp_dialog.getDatePicker()

+0

沒有robotium是它工作正常? – itsrajesh4uguys

+0

是的,它確實...... – SacGax

+0

你是否用可能干擾資源加載/佈局膨脹的方式來裝備你的'Context'? – laalto

回答

0

好了,事情是我是一個NullPointerException即使在片段被顯示之前調用dp_frag.getDialog()也是如此。因此,我修改了我的代碼,如下所示:

 //wait for dialog fragment 
     solo.waitForFragmentByTag(DatePicker_Fragment.TAG); 

     //get a reference to dialog fragment 
     SherlockFragmentActivity current_activity = (SherlockFragmentActivity) solo.getCurrentActivity(); 
     DatePicker_Fragment dp_frag = (DatePicker_Fragment) current_activity.getSupportFragmentManager().findFragmentByTag(DatePicker_Fragment.TAG); 

     //wait until dialog fragment is visible on screen 
     assertNotNull(dp_frag); 
     while(!(dp_frag.isShown())){ 
      Log.w(getName(), "Dialog fragment visible " + dp_frag.isShown()); 
      solo.sleep(100); 
     } 

     //set the start date 
     DatePickerDialog dp_dialog = (DatePickerDialog) dp_frag.getDialog(); 
     assertNotNull(dp_dialog); 
     DatePicker datePicker = dp_dialog.getDatePicker(); 
     solo.setDatePicker(datePicker,2013,5,21); 
     solo.clickOnView(dp_dialog.getButton(DatePickerDialog.BUTTON_POSITIVE)); 

,並在DatePickerDialogFragment我添加下面的方法:

public boolean isShown(){ 
    if(getDialog() == null){ 
     return false; 
    }else{ 
     return true; 
    } 
} 
相關問題