在Android library中不能使用switch-case语句访问资源ID的原因分析及解决方案


报错是因为case分支后面跟的参数必须是常数,也就是说library中的R.java的资源ID不是常数,
public static final class animator {
public static int design_appbar_state_list_animator = 0x7f020001;
}
public static final class array {
public static int card_tabList = 0x7f030001;
}
public static final class attr {
public static int actionBarDivider = 0x7f040001;
public static int actionBarItemBackground = 0x7f040002;
public static int actionBarPopupTheme = 0x7f040003;
...}

如上面所示每一个资源ID都没有被声明为final
项目下的R.java的资源ID如下
public static final class animator {
public static final int design_appbar_state_list_animator=0x7f020000;
}
public static final class array {
public static final int card_tabList=0x7f030000;
public static final int tab_list=0x7f030001;
}

查了很多资料,除了用if-else替换没有其他解决方案[Facepalm]

  

posted @ 2017-12-07 23:02  夏落若的博客  阅读(887)  评论(0编辑  收藏  举报