在libs上切换大小写错误

时间:2018-03-10 12:41:48

标签: java android

我在向Android应用程序实现库模块时出错。 它说:

Constant expression required Resource IDs cannot be used in a switch statement in Android library modules less... (Ctrl+F1) 
Validates using resource IDs in a switch statement in Android library module. Resource IDs are non final in the library projects since SDK tools r14, means that the library code cannot treat these IDs as constants.

这是我的代码:

private void changeWeekStart(@IdRes int selection) {
        switch (selection) {
            case R.id.action_week_start_saturday:
                CalendarUtils.sWeekStart = Calendar.SATURDAY;
                break;
            case R.id.action_week_start_sunday:
                CalendarUtils.sWeekStart = Calendar.SUNDAY;
                break;
            case R.id.action_week_start_monday:
                CalendarUtils.sWeekStart = Calendar.MONDAY;
                break;
        }
        PreferenceManager.getDefaultSharedPreferences(this)
                .edit()
                .putInt(CalendarUtils.PREF_WEEK_START, CalendarUtils.sWeekStart)
                .apply();
        supportInvalidateOptionsMenu();
        mCoordinator.reset();
    }

请帮助我从switch-case更改为if-else代码。 感谢。

1 个答案:

答案 0 :(得分:0)

你到处都有case,这将成为if陈述的条件; break语句将与}语句中的结束if对应。

if (selection == R.id.action_week_start_saturday) {
    CalendarUtils.sWeekStart = Calendar.SATURDAY;
} else if (selection == R.id.action_week_start_sunday) {
    CalendarUtils.sWeekStart = Calendar.SUNDAY;
} else if (selection == R.id.action_week_start_monday) {
    CalendarUtils.sWeekStart = Calendar.MONDAY;
}

请注意,您不会在switch-case块中提供默认大小写,因此如果CalendarUtils.sWeekStart参数的值与您考虑的值不同,则selection可能不会被修改

相关问题