当在kitkat上设置minDate或maxDate时,Android DatePicker崩溃

你好我有一个日期选择器对话框,当用户点击一个EditText时弹出。 它在另一个设备上工作,我尝试过运行Android 6.0,但是当我尝试运行kitkat的表上的同一个应用程序时,它崩溃了。

我可以确认它是setMinDate或setMaxDate代码,因为我把它拿出来,并且在我做完之后页面没有崩溃。

这是日志:

E / AndroidRuntime:致命例外:主

进程:com.xxxxxx.pointofsale,PID:13402 android.widget.CalendarView.onScroll(CalendarView.java:1216)上的java.lang.NullPointerException在android.widget.CalendarView.access $ 900(CalendarView.java:76)。 widget.CalendarView $ 2.onScroll(CalendarView.java:1077)在android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:1463)在android.widget.ListView.layoutChildren(ListView.java:1745)在android.widget.AbsListView。 onLayout(AbsListView.java:2149)at android.view.View.layout(View.java:15125)at android.view.ViewGroup.layout(ViewGroup.java:4862)at android.widget.LinearLayout.setChildFrame(LinearLayout.java) :1888)at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)at android.view.View.layout(View.java:15125)at android .view.ViewGroup.layout(ViewGroup.java:4862)at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)at android.widget.FrameLayout.onLayout(FrameLayout.java:450) )在android.widget上的android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)上的android.view.ViewGroup.layout(ViewGroup.java:4862)上的android.view.View.layout(View.java:15125) .LinearLayout.layoutHorizo​​ntal(LinearLayout.java:1877)at android.widget.LinearLayout.onLayout(LinearLayout.java:1653)at android.view.View.layout(View.java:15125)at android.view.ViewGroup.layout() ViewGroup.java:4862)在android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)在android.widget.FrameLayout.onLayout(FrameLayout.java:450)在android.view.View.layout(View.java:15125 )在android.view.ViewGroup.layout(ViewGroup.java:4862)在android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)在android.widget.FrameLayout.onLayout(FrameLayout.java:450)在android.view .View.layout(View.java:15125)在android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)在android.widget.FrameLayout.onLayout(android.view.ViewGroup.layout(ViewGroup.java:4862) FrameLayout.java:450)在android.vi ew.View.layout(View.java:15125)在android.view.ViewGroup.layout(ViewGroup.java:4862)在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)在android.widget.LinearLayout.layoutVertical (LinearLayout.java:1742)在android.view.View.config.ViewGroup.layout(ViewGroup.java:android.view.View.layout(View.java:15125))android.widget.LinearLayout.onLayout(LinearLayout.java:1651) 4862)在android.android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)android.widget.FrameLayout.onLayout(FrameLayout.java:450)在android.view.View.layout(View.java:15125)在android。 view.ViewGroup.layout(ViewGroup.java:4862)在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)在android.widget.LinearLayout.onLayout (LinearLayout.java:1651)at android.view.View.layout(View.java:15125)at android.view.ViewGroup.layout(ViewGroup.java:4862)at android.widget.FrameLayout.layoutChildren(FrameLayout.java: 515)在android.widge t.FrameLayout.onLayout(FrameLayout.java:450)at android.view.View.layout(View.java:15125)at android.view.ViewGroup.layout(ViewGroup.java:4862)at android.view.ViewRootImpl.performLayout (ViewRootImpl.java:2323)在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2029)在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1192)在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl。 java:6231)在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:788)在android.view.Choreographer.doCallbacks(Choreographer.java:591)在android.view.Choreographer.doFrame(Choreographer.java:560) )at android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:774)at android.os.Handler.handleCallback(Handler.java:808)at android.os.Handler.dispatchMessage(Handler.java:103)at android .os.Looper.loop(Looper.java:193)at android.app.ActivityThread.main(ActivityThread.java:5292)at java.lang.reflect.Method.invokeNative(Nativ

这是我的代码:

  final DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {

            mCalendar.set(Calendar.YEAR, year);
            mCalendar.set(Calendar.MONTH, monthOfYear);
            mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            String myFormat = "yyyy-MM-dd"; //In which you need put here
            SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

            mSelectedDate = sdf.format(mCalendar.getTime());
            etDate.setText(Utils.formatDate(mSelectedDate, "yyyy-MM-dd", "EEEE, d MMMM yyyy"));
            getTravelSchedules();
        }

    };

    private final View.OnClickListener mDateViewOnClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (etDate.getText().toString().equals("")) {
                Toast.makeText(getApplicationContext(), "Please select a destination first!", Toast.LENGTH_LONG).show();
                return;
            }

            Date today = new Date();
            Calendar c = Calendar.getInstance();
            c.setTime(today);
            c.add(Calendar.DATE, +7);
            final long maxDate = c.getTime().getTime();

            c.setTime(today);
            final long minDate = c.getTime().getTime();

            DatePickerDialog datePickerDialog = new DatePickerDialog(mContext, R.style.DialogTheme, mDateSetListener, mCalendar
                    .get(Calendar.YEAR), mCalendar.get(Calendar.MONTH),
                    mCalendar.get(Calendar.DAY_OF_MONTH));
            datePickerDialog.getDatePicker().setMinDate(minDate);
            datePickerDialog.getDatePicker().setMaxDate(maxDate);
            datePickerDialog.show();
        }
    };

你也可以使用这个功能

void showDatePickerDialog(final EditText et) {

    DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            // TODO Auto-generated method stub
            Calendar myCalendar = Calendar.getInstance();
            myCalendar.set(Calendar.YEAR, year);
            myCalendar.set(Calendar.MONTH, monthOfYear);
            myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
           et.setText(new SimpleDateFormat("dd MMM, yyyy", Locale.getDefault()).format(myCalendar.getTime()));     
        }
    };

    // Calendar Minimum Range
    Calendar calendarMinDate = Calendar.getInstance();
    calendarMinDate.add(Calendar.DATE, 0);

   // Calendar Maximum Range
    Calendar calendarMaxDate = Calendar.getInstance();
    calendarMaxDate.add(Calendar.DATE, +7);

    DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),
            R.style.MyDatePickerDialogTheme, date, calYear, calMonth, calDay);
    datePickerDialog.getDatePicker().setMinDate(calendarMinDate.getTimeInMillis());
  datePickerDialog.getDatePicker().setMaxDate(calendarMaxDate.getTimeInMillis());
    datePickerDialog.show();
}
链接地址: http://www.djcxy.com/p/16639.html

上一篇: Android DatePicker crashes when setting minDate or maxDate on kitkat

下一篇: java.lang.IndexOutOfBoundsException when delete item twice