我是一个Android新手...
这是我的代码:
final Button taskDateButton = (Button)findViewById(R.id.taskTimeButton);
taskDateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialog datePickerDialog = new DatePickerDialog(TaskActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
taskDateCalendar.set(Calendar.YEAR, year);
taskDateCalendar.set(Calendar.MONTH, monthOfYear);
taskDateCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
taskDate = taskDateCalendar.getTime();
setDateButtonsWithDates();
TimePickerDialog timePickerDialog = new TimePickerDialog(TaskActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
taskDateCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
taskDateCalendar.set(Calendar.MINUTE, minute);
taskDate = taskDateCalendar.getTime();
setDateButtonsWithDates();
}
}, taskDateCalendar.get(Calendar.HOUR_OF_DAY), taskDateCalendar.get(Calendar.MINUTE), false);
timePickerDialog.show();
}
}, taskDateCalendar.get(Calendar.YEAR), taskDateCalendar.get(Calendar.MONTH), taskDateCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
}
});
加载DatePickerDialog时似乎崩溃了,特别是在这一行:
DatePickerDialog datePickerDialog = new DatePickerDialog(TaskActivity.this, new DatePickerDialog.OnDateSetListener() {
我环顾四周,但看不出是什么导致了这一点。
这是我得到的错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.Calendar.get(int)' on a null object reference
任何帮助?
由于
答案 0 :(得分:2)
您的taskDateCalendar
永远不会被初始化。
在使用之前尝试添加taskDateCalendar = Calendar.getInstance();
。
答案 1 :(得分:2)
您的taskDateCalendar
对象似乎为空。在taskDateCalendar = Calendar.getInstance()
之前致电taskDateCalendar.set(Calendar.YEAR, year)
。