【问题标题】:StackOverflowError while using Android Calendar's setMinDate()使用 Android 日历的 setMinDate() 时出现 StackOverflowError
【发布时间】:2020-01-09 14:02:23
【问题描述】:

参考上图中显示的错误。

当我设置calendarView.setMinDate 时出现这个问题 我无法解决我在使用日历时遇到了这个问题

后台并发复制 GC 释放了 98234(2MB) 个 AllocSpace 对象, 1(1564KB) LOS 对象,41% 空闲,34MB/58MB,共暂停 139us 366.099ms

这是我的代码

 public class select_time extends BaseActivity {

    @BindView(R.id.calendarView)
    CalendarView calendarView;
    DatesView datesView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_time);
        ButterKnife.bind(this);
        in = getIntent().getBooleanExtra("in", true);
        datesView = new DatesView(findViewById(R.id.datesView));
        datesView.setOnCalenderClick(new DatesView.onClenderClick() {
            @Override
            public void OnClick(boolean in) {
                setWanted(in);
            }
        });
        datesView.refresh(this);
        refreshCalendar();
        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
                Calendar calendar = Calendar.getInstance();
                calendar.set(year, month, day, 0, 0, 0);
                SharedPreferences prefs = getSharedPreferences("data", 0);
                prefs.edit().putLong((in ? "in" : "out") + "_date", calendar.getTimeInMillis()).apply();
                if (in) {
                    if (calendar.getTimeInMillis() == out_date.getTime() || calendar.getTimeInMillis() >= out_date.getTime()) {
                        calendar.add(Calendar.DATE, 1);
                        prefs.edit().putLong("out_date", calendar.getTimeInMillis()).apply();
                    }
                } else {
                    if (calendar.getTimeInMillis() == out_date.getTime() || calendar.getTimeInMillis() <= out_date.getTime()) {
                        calendar.add(Calendar.DATE, -1);
                        prefs.edit().putLong("in_date", calendar.getTimeInMillis()).apply();
                    }
                }
                datesView.refresh(select_time.this);
            }
        });
    }
    Date in_date,out_date;
    public void refreshCalendar(){
        SharedPreferences prefs = getSharedPreferences("data", 0);
        in_date = new Date(prefs.getLong("in_date", 0));
        out_date = new Date(prefs.getLong("out_date", 0));
        calendarView.setMinDate(System.currentTimeMillis() - 1000);
        calendarView.setDate(in ? in_date.getTime() : out_date.getTime());
        setWanted(in);

    }
    boolean in;
    public void setWanted(boolean in) {
        this.in = in;
        datesView.check_in.setBackgroundColor(getResources().getColor(in ? R.color.gray : R.color.white));
        datesView.check_out.setBackgroundColor(getResources().getColor(!in ? R.color.gray : R.color.white));
        refreshCalendar();
    }
    public void clickDone(View view) {
        finish();
    }
}

谁能帮我使用日历?

【问题讨论】:

  • 这是因为某处发生了递归。

标签: java android android-calendar


【解决方案1】:

您正在从refreshCalendar() 调用setWanted(in);,并且还从setWanted(boolean in) 调用refreshCalendar();。这会创建一个无限调用循环,最终耗尽堆栈。您需要删除这两个调用之一。

【讨论】:

    【解决方案2】:

    我想这是因为你在 setWanted 中调用 refreshCalendar();,它在 calenderOnChange 中调用,然后再次使用 datesView.refresh 调用,这就是你一次又一次调用它的原因,所以你需要删除这个循环. 可能是删除了 setWanted 中的refreshCalendar();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      • 2022-11-03
      • 1970-01-01
      相关资源
      最近更新 更多