DatePicker不工作,IllegalArgumentException

时间:2012-07-30 15:54:46

标签: android android-datepicker

我正在使用API​​级别10,我不想仅使用支持库来实现DatePicker,因此我使用以下方法创建日期对话框。我之前使用过一个DatePicker,它工作正常。我只是不知道这里有什么问题。

我有两个日期对话框,用于选择各个OnClickListeners调用的日期。有人可以帮忙吗?谢谢。

//这是在onCreate

 ImageView fromCalendar = (ImageView) findViewById(R.id.from_calendar);
ImageView toCalendar = (ImageView) findViewById(R.id.to_calendar);


fromCalendar.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        showDialog(FROM_DATE_DIALOG);

    }
});

toCalendar.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        showDialog(TO_DATE_DIALOG);

    }
});


 private DatePickerDialog.OnDateSetListener toDateSetListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {

        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        Log.e("mYear", "" + year + " mMonth:" + mMonth + " mDay" + mDay);
        String date = updateDisplay(true);

        toDate.setText(date);
    }
};

//在onCreate外面

private DatePickerDialog.OnDateSetListener fromDateSetListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {

        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        Log.e("mYear", "" + year + " mMonth:" + mMonth + " mDay" + mDay);
        String date = updateDisplay(true);

        fromDate.setText(date);
    }
};



protected Dialog onCreateDialog(int id) {
    switch(id) {

    case FROM_DATE_DIALOG:
        Log.e("onCreateDialog::mYear", "" + mYear + " mMonth:" + mMonth
                + " mDay" + mDay);
        return new DatePickerDialog(this, fromDateSetListener, mYear, mMonth,
                mDay);


    case TO_DATE_DIALOG:
        Log.e("onCreateDialog::mYear", "" + mYear + " mMonth:" + mMonth
                + " mDay" + mDay);
        return new DatePickerDialog(this, toDateSetListener, mYear, mMonth,
                mDay);
    }
    return null; 
}

1 个答案:

答案 0 :(得分:1)

对不起,这是重复的!

java.lang.IllegalArgumentException: current should be >= start and <= end

我只需要初始化mYear,mMonth和mDay。