我正在使用datepickerdialog.它可以在kitkat上正常运行,但是当我在棒棒糖上运行应用程序时,当我单击编辑文本时,它会打开一个datepickerdialog框,但是当我选择日期时,它会给出错误的停止错误.以下是edittext上datepicker的代码.
private voID setDateTimeFIEld() { fromLabel.setonClickListener(this); tolabel.setonClickListener(this); final DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); //yyyy/MM/dd HH:mm:ss final Date date = new Date(); final String u = dateFormat.format(date); Calendar newCalendar = Calendar.getInstance(); fromDatePickerDialog = new DatePickerDialog(this, new OnDateSetListener() { public voID onDateSet(DatePicker vIEw, int year, int monthOfYear, int dayOfMonth) { Calendar newDate = Calendar.getInstance(); newDate.set(year, monthOfYear, dayOfMonth); from1 = dateFormatter.format(newDate.getTime()); diff1 = newDate.getTimeInMillis(); long d = date.getTime(); if((newDate.getTime()).equals(date)||(newDate.getTime()).after(date)){ long d1 = (diff1 / (24 * 60 * 60 * 1000) - d / (24 * 60 * 60 * 1000)) + 1; if(d1>30){ total.setVisibility(VIEw.VISIBLE); total.setText("Booking not allowed as the Date given is outsIDe Advance Booking Period"); avail.setVisibility(VIEw.GONE); } else{ total.setVisibility(VIEw.GONE); fromLabel.setText(from1); tolabel.setText(null); to=null; avail.setVisibility(VIEw.GONE); from=fromLabel.getText().toString(); } } else{ total.setVisibility(VIEw.VISIBLE); total.setText("Choose date after or equals to current date"); fromLabel.setText(""); tolabel.setText(null); from=null; to=null; } } },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH)); fromDatePickerDialog.setbutton(DialogInterface.button_POSITIVE, getString(R.string.Done), new DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog, int which) { if (which == DialogInterface.button_POSITIVE) { dialog.cancel(); if(type.equals("According to time")) { int cnt=-1; if(from1.equals(u)){ cnt = 1; loadTimeSpinnerDataATT(text,from,cnt); } else if(total.getText()=="Choose date after or equals to current date") { } else if(total.getText()=="Booking not allowed as the Date given is outsIDe Advance Booking Period") { } else {cnt = 0; loadTimeSpinnerDataATT(text,from,cnt); } } } } }); } public voID onClick(VIEw vIEw) { if(vIEw == fromLabel) { fromDatePickerDialog.show(); } else if(vIEw == tolabel) { toDatePickerDialog.show(); } } public voID onClose(DialogInterface dialogInterface) { } }
解决方法:
试试吧,可能对您有帮助,
在这里改写了日期选择器错误
Link For Date picker
import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import androID.support.v7.app.ActionBaractivity;import androID.text.inputType;import androID.app.DatePickerDialog;import androID.app.Dialog;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.@[email protected];import androID.@[email protected];public class MainActivity extends ActionBaractivity { private int year; private int month; private int day; static final int DATE_PICKER_ID = 1111; // for date picker EditText m3_Datedisplay; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); m3_Datedisplay = (EditText) findVIEwByID(R.ID.datepick); // Get current date by calender final Calendar c = Calendar.getInstance(); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); // Show selected date StringBuilder dateValue1 = new StringBuilder().append(day).append("-") .append(month + 1).append("-").append(year).append(" "); // for Converting Correct Date format Save into Database SimpleDateFormat sdf123 = new SimpleDateFormat("dd-MM-yyyy"); String abs1 = dateValue1.toString(); Date testDate1 = null; try { testDate1 = sdf123.parse(abs1); } catch (ParseException e) { e.printstacktrace(); } SimpleDateFormat formatter1 = new SimpleDateFormat("dd-MM-yyyy"); String DateFormat = formatter1.format(testDate1); m3_Datedisplay.setText(DateFormat); m3_Datedisplay.setFocusable(false); m3_Datedisplay.setinputType(inputType.TYPE_NulL); m3_Datedisplay.setonClickListener(new VIEw.OnClickListener() { @SuppressWarnings("deprecation") @OverrIDe public voID onClick(VIEw v) { showDialog(DATE_PICKER_ID); } }); } @OverrIDe protected Dialog onCreateDialog(int ID) { switch (ID) { case DATE_PICKER_ID: // open datepicker dialog. // set date picker for current date // add pickerListener Listner to date picker // return new DatePickerDialog(this, pickerListener, year, month, // day); // ///Only Show till Date Not More than That. DatePickerDialog dialog = new DatePickerDialog(this, pickerListener, year, month, day); dialog.getDatePicker().setMaxDate(new Date().getTime()); return dialog; } return null; } private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() { // when dialog Box is closed, below method will be called. @OverrIDe public voID onDateSet(DatePicker vIEw, int selectedYear, int selectedMonth, int selectedDay) { year = selectedYear; month = selectedMonth; day = selectedDay; // Show selected date StringBuilder dateValue = new StringBuilder().append(day) .append("-").append(month + 1).append("-").append(year) .append(" "); // for Converting Correct Date format Save into Database SimpleDateFormat sdf123 = new SimpleDateFormat("dd-MM-yyyy"); String abs1 = dateValue.toString(); Date testDate1 = null; try { testDate1 = sdf123.parse(abs1); } catch (ParseException e) { e.printstacktrace(); } SimpleDateFormat formatter1 = new SimpleDateFormat("dd-MM-yyyy"); String DateFormat = formatter1.format(testDate1); m3_Datedisplay.setText(DateFormat); } };}
更改清单中的最小API-11
总结以上是内存溢出为你收集整理的Java-棒棒糖中的Android Edittext Onclick Datepickerdialog错误全部内容,希望文章能够帮你解决Java-棒棒糖中的Android Edittext Onclick Datepickerdialog错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)