我正在设置一个arraylist的日期范围。 当最小和最大日期不同时,它工作正常。
当两个日期相同时,问题就出现了。
即使最小和最大日期相同,是否有可能以某种方式设置日期范围?
//---- Set Date Range------//
SimpleDateFormat sDateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date fromDateRange = null ;
Date toDateRange = null ;
try {
fromDateRange = sDateFormat.parse(dateFilters.get(i).getFromDate());
toDateRange = sDateFormat.parse(dateFilters.get(i).getToDate());
} catch (ParseException e) {}
fromDateFragment.setDateRange(fromDateRange.getTime(), toDateRange.getTime()); // setDateRange is a custom method of my DatePickerFragment class .
//------------------------//
DatePickerFragment.java
package com.example.component;
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.widget.DatePicker;
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
onDateSet mOnDateSet;
DatePickerDialog datePickerDialog;
public DatePickerFragment(Context context){
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
datePickerDialog = new DatePickerDialog(context, this, year, month, day);
}
public DatePickerFragment(){}
public void setDateListener(onDateSet mOnDateSet) {
this.mOnDateSet = mOnDateSet;
}
public interface onDateSet {
public void onDate(int year, int monthOfYear,
int dayOfMonth);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
return datePickerDialog;
}
public void setDateRange(long minDate, long maxDate){
datePickerDialog.getDatePicker().setMinDate(minDate);
datePickerDialog.getDatePicker().setMaxDate(maxDate);
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
if(view.isShown()){
dismiss();
mOnDateSet.onDate(year, monthOfYear+1, dayOfMonth);
}
}
}
Logcat: -
02-25 02:08:04.569: E/AndroidRuntime(8385): FATAL EXCEPTION: main
02-25 02:08:04.569: E/AndroidRuntime(8385): java.lang.NullPointerException
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.CalendarView.onScroll(CalendarView.java:1224)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.CalendarView.access$900(CalendarView.java:76)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.CalendarView$2.onScroll(CalendarView.java:1085)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:1520)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.ListView.layoutChildren(ListView.java:1707)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.AbsListView.onLayout(AbsListView.java:2300)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.View.layout(View.java:14067)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.ViewGroup.layout(ViewGroup.java:4376)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.View.layout(View.java:14067)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.ViewGroup.layout(ViewGroup.java:4376)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.View.layout(View.java:14067)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.ViewGroup.layout(ViewGroup.java:4376)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1644)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.LinearLayout.onLayout(LinearLayout.java:1428)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.View.layout(View.java:14067)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.ViewGroup.layout(ViewGroup.java:4376)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.View.layout(View.java:14067)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.ViewGroup.layout(ViewGroup.java:4376)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.View.layout(View.java:14067)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.ViewGroup.layout(ViewGroup.java:4376)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.View.layout(View.java:14067)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.ViewGroup.layout(ViewGroup.java:4376)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.View.layout(View.java:14067)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.ViewGroup.layout(ViewGroup.java:4376)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.View.layout(View.java:14067)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.ViewGroup.layout(ViewGroup.java:4376)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.View.layout(View.java:14067)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.ViewGroup.layout(ViewGroup.java:4376)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.View.layout(View.java:14067)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.ViewGroup.layout(ViewGroup.java:4376)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1986)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1807)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4464)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.Choreographer.doFrame(Choreographer.java:525)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.os.Handler.handleCallback(Handler.java:615)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.os.Handler.dispatchMessage(Handler.java:92)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.os.Looper.loop(Looper.java:137)
02-25 02:08:04.569: E/AndroidRuntime(8385): at android.app.ActivityThread.main(ActivityThread.java:4895)
02-25 02:08:04.569: E/AndroidRuntime(8385): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 02:08:04.569: E/AndroidRuntime(8385): at java.lang.reflect.Method.invoke(Method.java:511)
02-25 02:08:04.569: E/AndroidRuntime(8385): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
02-25 02:08:04.569: E/AndroidRuntime(8385): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
02-25 02:08:04.569: E/AndroidRuntime(8385): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
前几天我也遇到了同样的问题..
我为特定日期设置了最短和最长时间,并为我工作正常。
Calendar mCalendarMax = Calendar.getInstance();
// Set your day - month - year here
mCalendarMax.set(Integer.parseInt(dateMaxData[2]), Integer.parseInt(dateMaxData[1]), Integer.parseInt(dateMaxData[0]));
mCalendarMax.set(Calendar.HOUR_OF_DAY, mCalendarMax.getMaximum(Calendar.HOUR_OF_DAY));
mCalendarMax.set(Calendar.MINUTE, mCalendarMax.getMaximum(Calendar.MINUTE));
mCalendarMax.set(Calendar.SECOND, mCalendarMax.getMaximum(Calendar.SECOND));
mCalendarMax.set(Calendar.MILLISECOND, mCalendarMax.getMaximum(Calendar.MILLISECOND));
Calendar mCalendarTo = Calendar.getInstance();
// Set your day - month - year here
mCalendarTo.set(Integer.parseInt(dateData[2]),Integer.parseInt(dateData[1]),Integer.parseInt(dateData[0]));
mCalendarTo.set(Calendar.HOUR_OF_DAY, mCalendarTo.getMinimum(Calendar.HOUR_OF_DAY));
mCalendarTo.set(Calendar.MINUTE, mCalendarTo.getMinimum(Calendar.MINUTE));
mCalendarTo.set(Calendar.SECOND, mCalendarTo.getMinimum(Calendar.SECOND));
mCalendarTo.set(Calendar.MILLISECOND, mCalendarTo.getMinimum(Calendar.MILLISECOND));
使用上述日历的毫秒而不是日期
fromDateFragment.setDateRange(mCalendarTo.getTimeInMillis(), mCalendarMax.getTimeInMillis());