Android DatePicker crashes when setting minDate or maxDate on kitkat

Hello I have a datepicker dialog that pops up when the user taps on an EditText. It works on another device I tried it on which run Android 6.0 however when I tried the same app on a table that run kitkat it crashed.

I can confirm it is the setMinDate or setMaxDate code because I took that out and the page didn't crash after I did.

This is the log:

E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.xxxxxx.pointofsale, PID: 13402 java.lang.NullPointerException at android.widget.CalendarView.onScroll(CalendarView.java:1216) at android.widget.CalendarView.access$900(CalendarView.java:76) at android.widget.CalendarView$2.onScroll(CalendarView.java:1077) at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:1463) at android.widget.ListView.layoutChildren(ListView.java:1745) at 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 ) 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.layoutHorizontal(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) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515) at android.widget.FrameLayout.onLayout(FrameLayout.java:450) 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) 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) at android.vi ew.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) 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.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) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2029) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1192) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6231) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788) at android.view.Choreographer.doCallbacks(Choreographer.java:591) at 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

This is my code:

  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/16640.html

上一篇: Android的RelativeLayout:如何alignParentBottom包装在一个ScrollView?

下一篇: 当在kitkat上设置minDate或maxDate时,Android DatePicker崩溃