我在自定义对话框中创建了一个日期选择器,而不像通常人们那样在DatepickerDialog中创建。 问题是当我尝试限制过去的日期时,在运行时应用程序崩溃时,日志cat out i附加。我的代码[附件]可能会遗漏一些内容,请帮忙。
04-30 10:24:16.278: D/dalvikvm(19320): GC_CONCURRENT freed 1764K, 27% free 9106K/12412K, paused 2ms+2ms, total 32ms
04-30 10:24:16.278: D/dalvikvm(19320): WAIT_FOR_CONCURRENT_GC blocked 22ms
04-30 10:24:16.282: E/AndroidRuntime(19320): FATAL EXCEPTION: main
04-30 10:24:16.282: E/AndroidRuntime(19320): java.lang.StackOverflowError
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.text.SpannableStringBuilder.getChars(SpannableStringBuilder.java:913)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.text.TextUtils.getChars(TextUtils.java:75)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.text.method.ReplacementTransformationMethod$ReplacementCharSequence.getChars(ReplacementTransformationMethod.java:151)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.text.TextUtils.getChars(TextUtils.java:75)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.text.TextUtils.indexOf(TextUtils.java:108)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.text.StaticLayout.generate(StaticLayout.java:182)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.text.DynamicLayout.reflow(DynamicLayout.java:288)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.text.DynamicLayout.<init>(DynamicLayout.java:174)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.TextView.makeSingleLayout(TextView.java:5952)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.TextView.makeNewLayout(TextView.java:5850)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.TextView.checkForRelayout(TextView.java:6391)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.TextView.setText(TextView.java:3672)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.TextView.setText(TextView.java:3530)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.EditText.setText(EditText.java:80)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.TextView.setText(TextView.java:3505)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.NumberPicker.updateInputTextView(NumberPicker.java:1763)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.NumberPicker.setDisplayedValues(NumberPicker.java:1381)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.updateSpinners(DatePicker.java:642)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.updateDate(DatePicker.java:545)
04-30 10:24:16.282: E/AndroidRuntime(19320): at com.view.MyProject.SchedulesActivity$28.onDateChanged(SchedulesActivity.java:1328)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.notifyDateChanged(DatePicker.java:699)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.updateDate(DatePicker.java:547)
04-30 10:24:16.282: E/AndroidRuntime(19320): at com.view.MyProject.SchedulesActivity$28.onDateChanged(SchedulesActivity.java:1328)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.notifyDateChanged(DatePicker.java:699)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.updateDate(DatePicker.java:547)
04-30 10:24:16.282: E/AndroidRuntime(19320): at com.view.MyProject.SchedulesActivity$28.onDateChanged(SchedulesActivity.java:1328)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.notifyDateChanged(DatePicker.java:699)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.updateDate(DatePicker.java:547)
04-30 10:24:16.282: E/AndroidRuntime(19320): at com.view.MyProject.SchedulesActivity$28.onDateChanged(SchedulesActivity.java:1328)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.notifyDateChanged(DatePicker.java:699)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.updateDate(DatePicker.java:547)
04-30 10:24:16.282: E/AndroidRuntime(19320): at com.view.MyProject.SchedulesActivity$28.onDateChanged(SchedulesActivity.java:1328)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.notifyDateChanged(DatePicker.java:699)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.updateDate(DatePicker.java:547)
04-30 10:24:16.282: E/AndroidRuntime(19320): at com.view.MyProject.SchedulesActivity$28.onDateChanged(SchedulesActivity.java:1328)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.notifyDateChanged(DatePicker.java:699)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.updateDate(DatePicker.java:547)
04-30 10:24:16.282: E/AndroidRuntime(19320): at com.view.MyProject.SchedulesActivity$28.onDateChanged(SchedulesActivity.java:1328)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.notifyDateChanged(DatePicker.java:699)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.updateDate(DatePicker.java:547)
04-30 10:24:16.282: E/AndroidRuntime(19320): at com.view.MyProject.SchedulesActivity$28.onDateChanged(SchedulesActivity.java:1328)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.notifyDateChanged(DatePicker.java:699)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.updateDate(DatePicker.java:547)
04-30 10:24:16.282: E/AndroidRuntime(19320): at com.view.MyProject.SchedulesActivity$28.onDateChanged(SchedulesActivity.java:1328)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.notifyDateChanged(DatePicker.java:699)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.updateDate(DatePicker.java:547)
04-30 10:24:16.282: E/AndroidRuntime(19320): at com.view.MyProject.SchedulesActivity$28.onDateChanged(SchedulesActivity.java:1328)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.notifyDateChanged(DatePicker.java:699)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.updateDate(DatePicker.java:547)
04-30 10:24:16.282: E/AndroidRuntime(19320): at com.view.MyProject.SchedulesActivity$28.onDateChanged(SchedulesActivity.java:1328)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.notifyDateChanged(DatePicker.java:699)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.updateDate(DatePicker.java:547)
04-30 10:24:16.282: E/AndroidRuntime(19320): at com.view.MyProject.SchedulesActivity$28.onDateChanged(SchedulesActivity.java:1328)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.notifyDateChanged(DatePicker.java:699)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.updateDate(DatePicker.java:547)
04-30 10:24:16.282: E/AndroidRuntime(19320): at com.view.MyProject.SchedulesActivity$28.onDateChanged(SchedulesActivity.java:1328)
04-30 10:24:16.282: E/AndroidRuntime(19320): at android.widget.DatePicker.notifyDateChanged(
代码:
DatePicker datePicker = (DatePicker)datePickerDialog.findViewById(R.id.datepicker);
getCurrDate();
datePicker.init(year, month, day, new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int selected_year, int selected_month,
int selected_day) {
// TODO Auto-generated method stub
/// getCurrDate();
if (selected_year < currYear)
view.updateDate(currYear, currMonth, currDay);
if (selected_month < currMonth && selected_year == currYear);
view.updateDate(currYear, currMonth, currDay);
if (selected_month < currDay && year == currYear && selected_month == currMonth)
view.updateDate(currYear, currMonth, currDay);
selectedDate = ""+(selected_month+1)+"/"+selected_day+"/"+selected_year;
datePicker_TV.setText(selectedDate);
repeatDlgDoneBtn.setTextColor(getResources().getColor(R.color.Dialog_cancel_blue));
repeatDlgDoneBtn.setEnabled(true);
getSelectedWeek();
}
});
datePicker.setCalendarViewShown(false);
datePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
datePickerDialog.show();
}
答案 0 :(得分:0)
抱歉,对于错误编程,无法在init中调用view.UpdateDate()(匿名内部类类型..) 这就是为什么崩溃在那里,虽然当DatePickerDialog对象被这样采用时,同样的逻辑是适用的:
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated
.................................
.................................
same logic as above code works for this scenario.
But i had used a Custom Dialog to increase UI Flexibility, so here's the perfect solution:
1. 1st get the Time in Millis for the Past Date[Called Min Date to be set in Date Picker]
2. If it is Current Date then do not use current time System.getCurrentTimeinMillis(); as it's always greater than the Actual date's Initial Time so using this will crash DatePicker. Instead send this current date to a date to time converter function as i had mentioned below:
public void getCurrDate()
{
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
return ""+(month+1)+"/"+day+"/"+year;
}
String input = getCurrDate();
Date date;
try {
date = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(input);
long milliseconds = date.getTime();
if(milliseconds>0){
datePicker.setMinDate(milliseconds);
//This Function will work fine, to set a future constraint use setMaxDate(long time);
}
else
{
//whatever else cond...
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}