gpt4 book ai didi

在 kitkat 上设置 minDate 或 maxDate 时,Android DatePicker 崩溃

转载 作者:行者123 更新时间:2023-11-29 02:34:15 24 4
gpt4 key购买 nike

您好,我有一个日期选择器对话框,当用户点击 EditText 时会弹出该对话框。它适用于另一台运行 Android 6.0 的设备,但当我在运行 kitkat 的 table 上尝试相同的应用程序时,它崩溃了。

我可以确认它是 setMinDate 或 setMaxDate 代码,因为我将其取出并且在我取出后页面没有崩溃。

这是日志:

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.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.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.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

这是我的代码:

  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();
}

关于在 kitkat 上设置 minDate 或 maxDate 时,Android DatePicker 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47957564/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com