gpt4 book ai didi

java - MaterialDatePicker,选择结束日期后应用程序崩溃

转载 作者:行者123 更新时间:2023-12-05 06:52:20 24 4
gpt4 key购买 nike

我有一个选择数据范围的 MaterialDatePicker,例如从 2021 年 1 月 1 日到 2021 年 1 月 31 日

MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();

CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();

builder.setCalendarConstraints(constraintsBuilder.build());

int dialogTheme = resolveOrThrow(requireContext());

builder.setTheme(dialogTheme);

MaterialDatePicker<Pair<Long, Long>> picker = builder.build();

picker.show(requireActivity().getSupportFragmentManager(), picker.toString());

picker.addOnPositiveButtonClickListener(selection -> {

long startDate = selection.first;
long endDate = selection.second;

from = DateFormat.format("yyyy-MM-dd", new Date(startDate)).toString();
to = DateFormat.format("yyyy-MM-dd", new Date(endDate)).toString();

});


private static int resolveOrThrow(Context context) {

TypedValue typedValue = new TypedValue();

if (context.getTheme().resolveAttribute(R.attr.materialCalendarTheme, typedValue, true)) {
return typedValue.data;
}

throw new IllegalArgumentException(context.getResources().getResourceName(R.attr.materialCalendarTheme));
}

打开 MaterialDatePicker 时,它会显示当前日期 2021 年 2 月 1 日。当我尝试从二月滚动到一月时,该月的最后一天不可见

enter image description here

要选择本月的最后一天,我应该向下滚动,但是当我选择本月的最后一天时,应用程序崩溃并显示此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getLeft()' on a null object referenceat com.google.android.material.datepicker.MaterialCalendarGridView.horizontalMidPoint(MaterialCalendarGridView.java:242)at com.google.android.material.datepicker.MaterialCalendarGridView.onDraw(MaterialCalendarGridView.java:172)at android.view.View.draw(View.java:23915)at android.widget.AbsListView.draw(AbsListView.java:4988)at android.view.View.updateDisplayListIfDirty(View.java:22790)at android.view.View.draw(View.java:23645)at android.view.ViewGroup.drawChild(ViewGroup.java:5336)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093)at android.view.View.updateDisplayListIfDirty(View.java:22776)at android.view.View.draw(View.java:23645)at android.view.ViewGroup.drawChild(ViewGroup.java:5336)at androidx.recyclerview.widget.RecyclerView.drawChild(RecyclerView.java:5030)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093)at android.view.View.draw(View.java:23918)at androidx.recyclerview.widget.RecyclerView.draw(RecyclerView.java:4429)at android.view.View.updateDisplayListIfDirty(View.java:22790)at android.view.View.draw(View.java:23645)at android.view.ViewGroup.drawChild(ViewGroup.java:5336)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093)at android.view.View.updateDisplayListIfDirty(View.java:22776)at android.view.View.draw(View.java:23645)at android.view.ViewGroup.drawChild(ViewGroup.java:5336)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093)at android.view.View.updateDisplayListIfDirty(View.java:22776)at android.view.View.draw(View.java:23645)at android.view.ViewGroup.drawChild(ViewGroup.java:5336)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093)at android.view.View.updateDisplayListIfDirty(View.java:22776)at android.view.View.draw(View.java:23645)at android.view.ViewGroup.drawChild(ViewGroup.java:5336)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093)at android.view.View.updateDisplayListIfDirty(View.java:22776)at android.view.View.draw(View.java:23645)at android.view.ViewGroup.drawChild(ViewGroup.java:5336)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093)at android.view.View.updateDisplayListIfDirty(View.java:22776)at android.view.View.draw(View.java:23645)at android.view.ViewGroup.drawChild(ViewGroup.java:5336)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093)at android.view.View.updateDisplayListIfDirty(View.java:22776)at android.view.View.draw(View.java:23645)at android.view.ViewGroup.drawChild(ViewGroup.java:5336)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093)at android.view.View.updateDisplayListIfDirty(View.java:22776)at android.view.View.draw(View.java:23645)at android.view.ViewGroup.drawChild(ViewGroup.java:5336)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093)at android.view.View.updateDisplayListIfDirty(View.java:22776)at android.view.View.draw(View.java:23645)at android.view.ViewGroup.drawChild(ViewGroup.java:5336)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093)at android.view.View.draw(View.java:23918)at com.android.internal.policy.DecorView.draw(DecorView.java:1268)at android.view.View.updateDisplayListIfDirty(View.java:22790)at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:567)at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:573)at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:650)at android.view.ViewRootImpl.draw(ViewRootImpl.java:4906)at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:4613)at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3754)at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2519)at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9775)2021-02-01 12:06:21.286 10369-10369/com.roko.hearth E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1010)at android.view.Choreographer.doCallbacks(Choreographer.java:809)at android.view.Choreographer.doFrame(Choreographer.java:744)at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:995)at android.os.Handler.handleCallback(Handler.java:938)at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:246)at android.app.ActivityThread.main(ActivityThread.java:8425)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:596)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

我试图查看 MaterialCalendarGridView.java:242,因为错误指示存在这一行:

  private static int horizontalMidPoint(@NonNull View view) {
return view.getLeft() + view.getWidth() / 2;
}

有什么帮助吗?

最佳答案

这是一个迟到的答案,但对其他人可能有用:崩溃已修复在 1.5.0-alpha01 版本的 android Material 组件上:https://github.com/material-components/material-components-android/releases/tag/1.5.0-alpha01 (检查 MaterialDatePicker 部分)

因此您可以将 Material 组件升级到 1.5.0-alpha01+ 或者解决方法是设置全屏主题

MaterialDatePicker.Builder.dateRangePicker()
.setTheme(R.style.ThemeOverlay_MaterialComponents_MaterialCalendar_Fullscreen)
.setCalendarConstraints(....)
.build()

关于java - MaterialDatePicker,选择结束日期后应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65991875/

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