使用Calendar.SATURDAY作为一周的最后一天,当全息日历崩溃 [英] Holo Calendar crashes when using Calendar.SATURDAY as last day of week

查看:1196
本文介绍了使用Calendar.SATURDAY作为一周的最后一天,当全息日历崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用霍洛日历库

有大量的code,所以我不能包括在这个线程。希望有人会看到这样谁使用它,但库是免费的,有在页面的底部,你可以打开一个已完成的项目的链接。

It is a large amount of code, and so I cannot include it in this thread. Hopefully someone will see this who has used it, but the library is free and there is a link at the bottom of the page to a completed project that you can open.

您可以设置日历的开始和结束日期通过以下方式:

You can set the start and end days of the calendar in the following way:

mMultiCalendarView.setFirstDayOfWeek(Calendar.MONDAY);
mMultiCalendarView.setLastDayOfWeek(Calendar.SUNDAY);

但是,这是一个看起来很奇怪的日历给我。一周的第一天是星期一,然后两个周末天处于本周结束。我想有每个星期去星期日至星期六。

But this is an odd-looking calendar to me. The first day of the week is Monday, and then both weekend days are at the end of the week. I would like to have each week go Sunday-Saturday.

我可以设置一周的任何东西的第一天,没有问题,但是下面一行:

I can set the first day of the week to anything, without problem, but the following line:

mMultiCalendarView.setLastDayOfWeek(Calendar.SATURDAY);

将导致应用程序来搪塞了,不管周的第一天。这似乎是在无限循环,日志猫不断喷出了类似以下内容:

causes the app to stall out, regardless of the first day of the week. It appears to be in an infinite loop, the log cat keeps spitting out similar the following:

02-10 20:29:03.876    2143-2143/(appName) I/dalvikvm-heap﹕ Clamp target GC heap from 96.710MB to 96.000MB
02-10 20:29:03.876    2143-2143/(appName) D/dalvikvm﹕ GC_FOR_ALLOC freed 1272K, 8% free 90712K/98260K, paused 45ms, total 45ms

直到应用程序崩溃与以下内存警告:

until the app crashes with the following memory warning:

FATAL EXCEPTION: main
java.lang.OutOfMemoryError

该错误指向库中的以下行:

The errors point to the following lines in the library:

at com.vdesmet.lib.calendar.CalendarView.createHeaders(CalendarView.java:286)
at com.vdesmet.lib.calendar.CalendarView.initView(CalendarView.java:93)
at com.vdesmet.lib.calendar.AbstractCalendarView.onLayout(AbstractCalendarView.java:397)

这是以下行,分别为:

which are the following lines, respectively:

LINE 286:    headers.addView(header);
LINE  93:    createHeaders();
LINE 397:    initView();

我道歉,这一切都意味着很少的没有手的库。我已经经历了code每一行梳理,和每一个循环处理一周的日子,但我不明白这一个。

I apologize that this all means very little without the libraries on hand. I've combed through every line of code, and every loop dealing days of the week, but I cannot figure this one out.

目前已与此人的工作,或有没有人有任何想法,以什么导致崩溃(基于该版本的这个Github上页)?

Has anyone worked with this, or does anyone have any thoughts to what's causing the crash (based on the library available at this Github page)?

推荐答案

展望从源头code,错误似乎是由无法访问的结束条件引起办,而循环,特别是在<一个href="https://github.com/vdesmet93/holo-calendar/blob/master/Calendar/src/main/java/com/vdesmet/lib/calendar/CalendarView.java#L291"相对=nofollow>行288-291 内 createHeaders()

Looking from the source code, the error seems to be caused by unreachable end condition for do-while loop, particularly on line 288-291 inside createHeaders():

private void createHeaders() {

    ...

    final int firstDayOfWeek = mFirstDayOfWeek;
    final int lastDayOfWeek = mLastDayOfWeek;

    ...

    int dayOfWeek = firstDayOfWeek;

    do {

        ...

        // increment dayOfWeek, make sure it's a valid day
        dayOfWeek = dayOfWeek % 7;
        dayOfWeek++;
    } while(dayOfWeek != lastDayOfWeek + 1);

    ...

}

Calendar.SATURDAY 的值是7,而不是6什么的开发商的预期。 (准确的说,它从 Calendar.SUNDAY 启动(1), Calendar.MONDAY (2),...至 Calendar.SATURDAY (7))。

Calendar.SATURDAY's value is 7, not 6 as what the developer expected. (To be exact, it starts from Calendar.SUNDAY (1), Calendar.MONDAY (2), ... to Calendar.SATURDAY (7)).

在另一方面,一周中的某天%7 只返回0-6,加1之后,你会得到1-7。但最终条件的循环时,一周中的某天== lastDayOfWeek + 1 。当它的 Calendar.SATURDAY ,它是(7 + 1)== 8 ,这是超出范围。因此,循环就不会结束,导致的OutOfMemoryError

On the other hand, dayOfWeek % 7 will only return 0-6, add 1 afterward and you will get 1-7. But the end condition for the loop is when dayOfWeek == lastDayOfWeek + 1. When it's Calendar.SATURDAY, it's (7 + 1) == 8, which is out of range. Thus, the loop won't end, resulting in OutOfMemoryError.

解决方案是最终状态更改为而(DAYOFWEEK =(lastDayOfWeek%7 + 1)!); 来确保该 lastDayOfWeek 1-7也换行。

The solution is to change the end condition to while(dayOfWeek != (lastDayOfWeek % 7 + 1)); to make sure that the lastDayOfWeek also wrap from 1-7.

附录

的变化需要应用2行内 CalendarView.java

The changes need to be applied on 2 lines inside CalendarView.java:

  • createHeaders(),<一个href="https://github.com/vdesmet93/holo-calendar/blob/master/Calendar/src/main/java/com/vdesmet/lib/calendar/CalendarView.java#L291"相对=nofollow>行291 :

} while(dayOfWeek != (lastDayOfWeek % 7 + 1));

  • initView(),<一个href="https://github.com/vdesmet93/holo-calendar/blob/master/Calendar/src/main/java/com/vdesmet/lib/calendar/CalendarView.java#L119"相对=nofollow>行119 :

    while((currentDay.get(Calendar.MONTH) + 1) % MONTHS_IN_YEAR == currentMonth ||
            currentDay.get(Calendar.MONTH) == currentMonth ||
            currentDay.get(Calendar.DAY_OF_WEEK) != (lastDayOfWeek % 7 + 1)) {
    

  • 感谢 pandes对他们的GitHub的问题跟进。

    这篇关于使用Calendar.SATURDAY作为一周的最后一天,当全息日历崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆