Robotium を使用してプロジェクトをテストしています。テストでは、datePickerDialog.getDatePicker() を呼び出すと、常に NullPointerExcpetion が返されます。
私の 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");
呼び出し時に常に NullPointerException を取得しますdp_dialog.getDatePicker()