简体   繁体   English

在第二次尝试打开日历时应用崩溃

[英]On second try to open calendar the app crash

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 问题是当我尝试第二次打开(打开->关闭->打开)时,应用崩溃, 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)

On CustomDateTimePicker.java:149 I have this selectedHour = calendar_date.get(Calendar.HOUR_OF_DAY); CustomDateTimePicker.java:149我已selectedHour = calendar_date.get(Calendar.HOUR_OF_DAY);此时间selectedHour = calendar_date.get(Calendar.HOUR_OF_DAY); 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();
}
}

In res.java:75 is this custom.showDialog(); 在res.java:75中是这个custom.showDialog();

findViewById(R.id.btnCalendar).setOnClickListener(
            new OnClickListener() {

                @Override
                public void onClick(View v) {

                    custom.showDialog();
                }

What can be the problem here? 这可能是什么问题? UPDATE 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();
                }

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. 您的问题似乎是关闭对话框后, calendar_date变为null ,或者calendar_date初始化为null且从未更改。

Based on the code you posted in the comments, the showDialog() function here and in the pastebin are not the same. 根据您在注释中发布的代码,此处和pastebin中的showDialog()函数并不相同。

In the code in the pastebin you do a check that is missing from this code and that's why you get a NullPointerException : 在pastebin中的代码中,您执行了此代码中缺少的检查,这就是为什么您得到NullPointerException的原因:

if (calendar_date == null) 
    calendar_date = Calendar.getInstance(); 

Adding the code above should solve your problem. 添加上面的代码应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM