android中的datepicker出错

时间:2013-06-24 08:58:40

标签: android datepicker

我创建了一个日期选择器,如图所示。它出现在我在选项卡上运行程序时,但当我尝试手机(HTC Glacier)应用程序崩溃时,我可以弄清楚我错过了什么。请帮帮我。谢谢enter image description here

这是我的logcat:

06-24 16:52:05.003: E/AndroidRuntime(19717): Caused by: java.lang.IllegalArgumentException: current should be >= start and <= end
06-24 16:52:05.003: E/AndroidRuntime(19717):    at android.widget.NumberPicker.setCurrent(NumberPicker.java:288)
06-24 16:52:05.003: E/AndroidRuntime(19717):    at     android.widget.DatePicker.updateSpinners(DatePicker.java:384)
06-24 16:52:05.003: E/AndroidRuntime(19717):    at android.widget.DatePicker.init(DatePicker.java:379)
06-24 16:52:05.003: E/AndroidRuntime(19717):    at ph.com.unilab.iReport.IreportMain.onCreate(IreportMain.java:85)

这是我在datepicker主要活动中的代码

 ....
calendar = Calendar.getInstance();
dp_Date = (DatePicker) findViewById(R.id.dp_Date);
dp_Date.init(80, calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), null);

dp_Date.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
dp_Date.init(2013, calendar.getTime().getMonth(), calendar.getTime().getDate(), new OnDateChangedListener()
{
  public void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
}
    });

....

2 个答案:

答案 0 :(得分:1)

您是否初始化了yearmonthOfYeardayOfMonth

private int mYear = 2012;
private int mMonth = 12;
private int mDay =12;

答案 1 :(得分:0)

您的初始化代码非常奇怪,并且使用了弃用的方法。 试试这个:

calendar = Calendar.getInstance();
datePicker = (DatePicker) findViewById(R.id.dp_Date);    
datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),   calendar.get(Calendar.DAY_OF_MONTH), new OnDateChangedListener() {
      public void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
      //TODO do stuff on date change
      }
});