我在向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
代码。
感谢。
答案 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
可能不会被修改