Android datepicker设置最小和最大日期不能一起使用

时间:2015-10-23 05:14:09

标签: android datepicker

我需要设置datepicker的最小和最大日期。

如果我使用setMinDate()函数或setMaxDate()函数,那么它可以正常工作。但如果我同时使用它们则会出错,活动停止。

dateFormatter = new SimpleDateFormat(Constant.DATE_FORMAT, Locale.US);

Calendar newCalendar = Calendar.getInstance();
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 3);

ConvinentDatePicker = new DatePickerDialog(this,null,
            newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

ConvinentDatePicker.setCancelable(true);
ConvinentDatePicker.setCanceledOnTouchOutside(true);
            ConvinentDatePicker.getDatePicker().setMinDate(newCalendar.getTimeInMillis());
ConvinentDatePicker.getDatePicker().setMaxDate(c.getTimeInMillis());
ConvinentDatePicker.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
            new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    timepicker.show();
                }
            });

ConvinentDatePicker.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
            new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                }
            });

 ConvinentDatePicker.show();

Log cat正在关注。提前致谢

10-23 11:03:54.966  12603-12603/com.kent.service.kentdealer E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.kent.service.kentdealer, PID: 12603
    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:1500)
            at android.widget.ListView.layoutChildren(ListView.java:1745)
            at android.widget.AbsListView.onLayout(AbsListView.java:2186)
            at android.view.View.layout(View.java:15147)
            at android.view.ViewGroup.layout(ViewGroup.java:4866)
            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:15147)
            at android.view.ViewGroup.layout(ViewGroup.java:4866)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
            at android.view.View.layout(View.java:15147)
            at android.view.ViewGroup.layout(ViewGroup.java:4866)
            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:15147)
            at android.view.ViewGroup.layout(ViewGroup.java:4866)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
            at android.view.View.layout(View.java:15147)
            at android.view.ViewGroup.layout(ViewGroup.java:4866)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
            at android.view.View.layout(View.java:15147)
            at android.view.ViewGroup.layout(ViewGroup.java:4866)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
            at android.view.View.layout(View.java:15147)
            at android.view.ViewGroup.layout(ViewGroup.java:4866)
            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:15147)
            at android.view.ViewGroup.layout(ViewGroup.java:4866)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
            at android.view.View.layout(View.java:15147)
            at android.view.ViewGroup.layout(ViewGroup.java:4866)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
            at android.view.View.layout(View.java:15147)
            at android.view.ViewGroup.layout(ViewGroup.java:4866)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
            at android.view.View.layout(View.java:15147)
            at android.view.ViewGroup.layout(ViewGroup.java:4866)
            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:5299)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.andr

1 个答案:

答案 0 :(得分:0)

试试这个,这从今天到datepicker对话框中的下一个5年日期显示。

public class MainActivity extends Activity {

    TextView txtDatePicker;
    Calendar cal,cal1;
    long maxDate;
    Date date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cal = Calendar.getInstance();
        cal1 = Calendar.getInstance();

        txtDatePicker = (TextView) findViewById(R.id.txtDatePicker);
        txtDatePicker.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                DatePickerDialog dialog = new DatePickerDialog(MainActivity.this, new OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
                        // TODO Auto-generated method stub
                        Toast.makeText(MainActivity.this, ""+arg1+"/"+(arg2+1)+"/"+arg3, Toast.LENGTH_SHORT).show();
                    }
            }, cal.YEAR, cal.MONTH, cal.DAY_OF_MONTH);
            dialog.getDatePicker().setMinDate(System.currentTimeMillis());
            cal1.add(Calendar.YEAR, 5);
            dialog.getDatePicker().setMaxDate(cal1.getTimeInMillis());
            dialog.show();
        }
    });
    }
}