Reputation: 499
I have found some strange for me bug in app. When I open calendar ( dialog window ) I can set date. The problem is when I try to open for second time ( open -> close -> open ) the app crash and Logcat
show this error
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)
On CustomDateTimePicker.java:149
I have this 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();
}
}
In res.java:75 is this custom.showDialog();
findViewById(R.id.btnCalendar).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
custom.showDialog();
}
What can be the problem here? UPDATE
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();
}
Upvotes: 1
Views: 221
Reputation: 35491
You problem seems to be that calendar_date
becomes null
after you close the dialog or calendar_date
is initialized to null
and never changed.
Based on the code you posted in the comments, the showDialog()
function here and in the pastebin are not the same.
In the code in the pastebin you do a check that is missing from this code and that's why you get a NullPointerException
:
if (calendar_date == null)
calendar_date = Calendar.getInstance();
Adding the code above should solve your problem.
Upvotes: 2