我想做什么,它存储从日期选择器中选择的日期,但是当我在选择日期后点击就绪按钮,崩溃,我不知道如何解决,我正在研究4.2.2
public class grafica extends ActionBarActivity {
public Calendar mCalendar;
private XYPlot mySimpleXYPlot;
private Button mDateButton;
private int mMonth;
private int mDay;
private int mYear;
static final int DATE_DIALOG_ID = 0;
static EditText DateEdit;
DialogFragment dateFragment;
private static final String DATE_FORMAT = "yyyy-MM-dd";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.grafica);
Button mDateButton = (Button) findViewById(R.id.fecha);
mDateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDatePickerDialog(v);
}
});
}
public void showDatePickerDialog(View v) {
dateFragment = new DatePickerFragment();
dateFragment.show(getSupportFragmentManager(), "datePicker");
}
class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void updateDateButtonText() {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
String dateForButton = dateFormat.format(mCalendar.getTime());
mDateButton.setText(dateForButton);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
mCalendar.set(Calendar.YEAR, year);
mCalendar.set(Calendar.MONTH, month);
mCalendar.set(Calendar.DAY_OF_MONTH, day);
updateDateButtonText();
DateEdit.setText(day + "/" + (month + 1) + "/" + year);
}
}
有控制台日志
08-27 12:56:03.045: E/AndroidRuntime(22404): FATAL EXCEPTION: main
08-27 12:56:03.045: E/AndroidRuntime(22404): Process: com.example.dietdfreev2, PID: 22404
08-27 12:56:03.045: E/AndroidRuntime(22404): java.lang.NullPointerException
08-27 12:56:03.045: E/AndroidRuntime(22404): at com.example.dietdfreev2.grafica$DatePickerFragment.onDateSet(grafica.java:116)
08-27 12:56:03.045: E/AndroidRuntime(22404): at android.app.DatePickerDialog.tryNotifyDateSet(DatePickerDialog.java:148)
08-27 12:56:03.045: E/AndroidRuntime(22404): at android.app.DatePickerDialog.onClick(DatePickerDialog.java:116)
08-27 12:56:03.045: E/AndroidRuntime(22404): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
08-27 12:56:03.045: E/AndroidRuntime(22404): at android.os.Handler.dispatchMessage(Handler.java:102)
08-27 12:56:03.045: E/AndroidRuntime(22404): at android.os.Looper.loop(Looper.java:136)
08-27 12:56:03.045: E/AndroidRuntime(22404): at android.app.ActivityThread.main(ActivityThread.java:5050)
08-27 12:56:03.045: E/AndroidRuntime(22404): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 12:56:03.045: E/AndroidRuntime(22404): at java.lang.reflect.Method.invoke(Method.java:515)
08-27 12:56:03.045: E/AndroidRuntime(22404): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-27 12:56:03.045: E/AndroidRuntime(22404): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
08-27 12:56:03.045: E/AndroidRuntime(22404): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您似乎没有初始化mCalendar
,即。 mCalendar
为null。将其初始化为
mCalendar = Calendar.getInstance();
将代码重写为
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.grafica);
mCalendar = Calendar.getInstance(); // add here
答案 1 :(得分:0)
您最初没有将日期设置到日期选择器对话框!!!
我认为这可能会有所帮助。
日历myCalendar = Calendar.getInstance(); DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
OnCreateView(){
datetext.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
new DatePickerDialog(getActivity(), date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
//更新点击日期 private void updateLabel(){ String myFormat =" yyyy-MM-dd&#34 ;; //你需要放在这里 SimpleDateFormat sdf = new SimpleDateFormat(myFormat,Locale.GERMAN); EditText datetext =(EditText)mainlayout .findViewById(R.id.ewbl_maintenance_date); datetext.setText(sdf.format(myCalendar.getTime())); }