我在app中发现了一些奇怪的bug。当我打开日历(对话框窗口)时,我可以设置日期。问题是当我尝试第二次打开(打开 - >关闭 - >打开)应用程序崩溃并Logcat
显示此错误
12-11 12:30:22.430: E/AndroidRuntime(1023): FATAL EXCEPTION: main
12-11 12:30:22.430: E/AndroidRuntime(1023): Process: com.res, PID: 1023
12-11 12:30:22.430: E/AndroidRuntime(1023): java.lang.NullPointerException
12-11 12:30:22.430: E/AndroidRuntime(1023): at com.res.CustomDateTimePicker.showDialog(CustomDateTimePicker.java:149)
12-11 12:30:22.430: E/AndroidRuntime(1023): at com.res.Reserv$2.onClick(Res.java:75)
12-11 12:30:22.430: E/AndroidRuntime(1023): at android.view.View.performClick(View.java:4438)
12-11 12:30:22.430: E/AndroidRuntime(1023): at android.view.View$PerformClick.run(View.java:18422)
12-11 12:30:22.430: E/AndroidRuntime(1023): at android.os.Handler.handleCallback(Handler.java:733)
12-11 12:30:22.430: E/AndroidRuntime(1023): at android.os.Handler.dispatchMessage(Handler.java:95)
12-11 12:30:22.430: E/AndroidRuntime(1023): at android.os.Looper.loop(Looper.java:136)
12-11 12:30:22.430: E/AndroidRuntime(1023): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-11 12:30:22.430: E/AndroidRuntime(1023): at java.lang.reflect.Method.invokeNative(Native Method)
12-11 12:30:22.430: E/AndroidRuntime(1023): at java.lang.reflect.Method.invoke(Method.java:515)
12-11 12:30:22.430: E/AndroidRuntime(1023): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-11 12:30:22.430: E/AndroidRuntime(1023): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-11 12:30:22.430: E/AndroidRuntime(1023): at dalvik.system.NativeStart.main(Native Method)
在CustomDateTimePicker.java:149
我有selectedHour = calendar_date.get(Calendar.HOUR_OF_DAY);
public void showDialog() {
if (!dialog.isShowing()) {
datePicker.setCalendarViewShown(false);
selectedHour = calendar_date.get(Calendar.HOUR_OF_DAY);
selectedMinute = calendar_date.get(Calendar.MINUTE);
timePicker.setIs24HourView(is24HourView);
timePicker.setCurrentHour(selectedHour);
timePicker.setCurrentMinute(selectedMinute);
datePicker.setMinDate(System.currentTimeMillis() + 345600000);
datePicker.updateDate(calendar_date.get(Calendar.YEAR),
calendar_date.get(Calendar.MONTH),
calendar_date.get(Calendar.DATE));
dialog.show();
btn_setDate.performClick();
}
}
在res.java:75中是custom.showDialog();
findViewById(R.id.btnCalendar).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
custom.showDialog();
}
这可能是什么问题? 更新
custom = new CustomDateTimePicker(this,
new CustomDateTimePicker.ICustomDateTimeListener() {
@Override
public void onSet(Dialog dialog, Calendar calendarSelected,
Date dateSelected, int year, String monthFullName,
String monthShortName, int monthNumber, int date,
String weekDayFullName, String weekDayShortName,
int hour24, int hour12, int min, int sec,
String AM_PM) {
((EditText) findViewById(R.id.datePicker)).setText(calendarSelected.get(Calendar.DAY_OF_MONTH)
+ "-" + (monthNumber+1) + "-" + year
+ " " + hour24 + ":" + min);
}
@Override
public void onCancel() {
}
});
/**
* Pass Directly current time format it will return AM and PM if you set
* false
*/
custom.set24HourFormat(false);
/**
* Pass Directly current data and time to show when it pop up
*/
custom.setDate(Calendar.getInstance());
findViewById(R.id.btnCalendar).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
custom.showDialog();
}
答案 0 :(得分:2)
关闭对话框或calendar_date
初始化为null
并且从未更改过后,您的问题似乎是calendar_date
变为null
。
根据您在评论中发布的代码,此处和pastebin中的showDialog()
函数不相同。
在pastebin中的代码中,您执行了此代码中缺少的检查,这就是您获得NullPointerException
的原因:
if (calendar_date == null)
calendar_date = Calendar.getInstance();
添加上述代码可以解决您的问题。