在Android library中不能使用switch-case语句访问资源ID的原因分析及解决方案
转自:http://www.jianshu.com/p/89687f618837
原因分析
当我们在Android依赖库中使用switch-case语句访问资源ID时会报如下图所示的错误,报的错误是case分支后面跟的参数必须是常数,换句话说出现这个问题的原因是Android library中生成的R.java中的资源ID不是常数:
打开library中的R.java,发现确实如此,每一个资源ID都没有被声明为final:

但是当你打开你的主工程,在onClick、onItemClick等各种回调方法中是可以通过switch-case语句来访问资源ID的,因为在主工程的R.java中资源ID都被声明为了final常量。
project中能够通过switch-case语句正常引用资源ID:

project中的R.java:

解决方案
既然是由于library的R.java中的资源ID不是常量引起的,我们可以在library中通过if-else-if条件语句来引用资源ID,这样就避免了这个错误:

参考资料:
为了进一步了解问题的具体原因,在万能的StackOverflow上还真搜到了这个问题:
In a regular Android project, constants in the resource R class are declared like this:
public static final int main=0x7f030004;
However, as of ADT 14, in a library project, they will be declared like this:
public static int main=0x7f030004;
In other words, the constants are not final in a library project. Therefore your code would no longer compile.
The solution for this is simple: Convert the switch statement into an if-else statement.
public void onClick(View src)
{
int id = src.getId();
if (id == R.id.playbtn){
checkwificonnection();
} else if (id == R.id.stopbtn){
Log.d(TAG, "onClick: stopping srvice");
Playbutton.setImageResource(R.drawable.playbtn1);
Playbutton.setVisibility(0); //visible
Stopbutton.setVisibility(4); //invisible
stopService(new Intent(RakistaRadio.this,myservice.class));
clearstatusbar();
timer.cancel();
Title.setText(" ");
Artist.setText(" ");
} else if (id == R.id.btnmenu){
openOptionsMenu();
}
}http://tools.android.com/tips/non-constant-fields
Tip
You can quickly convert a switch statement to an if-else statement using Eclipse's quick fix.Click on the switch keyword and press Ctrl + 1 then select
Convert 'switch' to 'if-else'.
问题详见:switch case statement error: case expressions must be constant expression
在Android library中不能使用switch-case语句访问资源ID的原因分析及解决方案的更多相关文章
- java中的Switch case语句
java中的Switch case 语句 在Switch语句中有4个关键字:switch,case break,default. 在switch(变量),变量只能是整型或者字符型,程序先读出这个变量的 ...
- 使用反射+策略模式代替项目中大量的switch case判断
我这里的业务场景是根据消息类型将离线消息存入mongoDB不同的collection中.其中就涉及到大量的分支判断,为了增强代码的可读性和可维护性,对之前的代码进行了重构. 先对比一下使用反射+策略模 ...
- 逆向知识第九讲,switch case语句在汇编中表达的方式
一丶Switch Case语句在汇编中的第一种表达方式 (引导性跳转表) 第一种表达方式生成条件: case 个数偏少,那么汇编中将会生成引导性的跳转表,会做出 if else的情况(类似,但还是能分 ...
- 为什么说在使用多条件判断时switch case语句比if语句效率高?
在学习JavaScript中的if控制语句和switch控制语句的时候,提到了使用多条件判断时switch case语句比if语句效率高,但是身为小白的我并没有在代码中看出有什么不同.去度娘找了半个小 ...
- switch… case 语句的用法
switch… case 语句的用法 public class Test7 { public static void main(String[] args) { int i=5; switch(i ...
- if语句,if...else if语句和switch...case语句的区别和分析
前段时间在工作中遇到了一个关于条件判断语句的问题,在if语句,if else if语句和switch case语句这三者之间分析,使用其中最有效率的一种方法. 所以就将这个问题作为自己第一篇博客的主要 ...
- Python | 基础系列 · Python为什么没有switch/case语句?
与我之前使用的所有语言都不同,Python没有switch/case语句.为了达到这种分支语句的效果,一般方法是使用字典映射: def numbers_to_strings(argument): sw ...
- 为什么switch...case语句比if...else执行效率高
在C语言中,教科书告诉我们switch...case...语句比if...else if...else执行效率要高,但这到底是为什么呢?本文尝试从汇编的角度予以分析并揭晓其中的奥秘. 第一步,写一个d ...
- JavaScript基础知识(if、if else、else if、while、switch...case语句)
13.语句 概念:就是分号(:) 代表一条语句的结束 习惯:一行只编写一条语句:一行编写多条语句(代码可读性较差) 语句块:可以包含多条语句 "{ }"将多条语句包裹 u ...
随机推荐
- Android lint 删除无用图片文件和配置文件
Android lint 删除无用.冗余的 配置文件和 图片资源 转载请注明 http://blog.csdn.net/aaawqqq?viewmode=contents Android项 ...
- OpenCV C++ 计算文件夹中对象文件数目及批量处理后保存到txt文件
//采用windows控制台实现计算文件夹中对象总数以及批量读取对象 //#include <afx.h> //和windows.h是一样的作用 #include <opencv2/ ...
- WebDriver 在使用 CSS Selector 与 XPath 在查找元素时如何取舍
开发在做Web系统时,用的是css div划分层,使用jQuery 选取元素.
- Android屏幕适配原理
几个概念: 1) 屏幕密度(dpi) :dot per inch,即每英寸像素数. ldpi(120),mdpi(160),hdpi(240),xhdpi(320) 计算方法: 以480x854,4. ...
- js将html5日期格式转为long型
var moveInArray = $("#move_in").val().split('-'); var d = new Date(); d.setYear(moveInArra ...
- css归纳
css文本框与按钮不对齐解决方案: 文字要包含在label标签中,并设置行高,否则文字会与文本框的顶 端对齐:文本框要设置vertical-align:middle;否则文本框与button顶端对齐: ...
- CSS:static/relative/absolute
static - default and this is the FLOW. ------------------------------------------------------------- ...
- datagridview 中添加了一个button类型的列,怎么写button的事件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 20145236 《Java程序设计》实验二实验报告
北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1452 指导教师:娄嘉鹏 实验日期:2016.04.08 实验名称: Java面向对象程序设计 实验内容: 初步掌握单元测试和T ...
- MVC扩展ValueProvider,通过实现IValueProvider接口创建SessionValueProvider
□ ValueProvider的大致工作原理 →通过Request.Form, Request.QueryString, Request.Files, RouteData.Values获取数据.→然后 ...