出现这个问题通常是因为代码中访问了数组中不存在的索引位置。解决方法有两个:
Button button = (Button) findViewById(R.id.button_date);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// 选择日期后的操作
}
}, year, month, dayOfMonth);
datePickerDialog.show();
}
});