Library Project里面使用Case语句判断R.id值报错。case expressions must be constant expressions
原文地址:http://blog.csdn.net/wchinaw/article/details/7325641
在一般的Android项目里R里面的资源声明看起来是这样的:
public static final int ...
但是在ADT14之后,声明是这样的
public static int ..
所有case语句换成if 就可以了
Non-constant Fields in Case Labels
|
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. The reason for this is simple: When multiple library projects are combined, the actual values of the fields (which must be unique) could collide. Before ADT 14, all fields were final, so as a result, all libraries had to have all their resources and associated Java code recompiled along with the main project whenever they were used. This was bad for performance, since it made builds very slow. It also prevented distributing library projects that didn't include the source code, limiting the usage scope of library projects. The reason the fields are no longer final is that it means that the
library jars can be compiled once and reused directly in other projects. As well as allowing distributing binary version of library projects (coming in r15), this makes for much faster builds. However, it has one impact on the source code of the library. Code of the following form will no longer compile: int id = view.getId();
switch (id) {
case R.id.button1: action1(); break; case R.id.button2: action2(); break; case R.id.button3: action3(); break;}That's because the
switch statement requires all the case labels, such asR.id.button1, to be constant at compile time (such that the values can be directlycopied into the .class files). The solution for this is simple: Convert the switch statement into
an if-else statement. Fortunately, this is very easy in Eclipse. Just place the caret on the switch keyword, and press Ctrl-1 (or Cmd-1 on Mac): ![]() In the above scenario, it will turn the
switch statement into this:int id = view.getId();
if (id == R.id.button1) {
action1();
} else if (id == R.id.button2) {
action2();
} else if (id == R.id.button3) {
action3();
}
This is typically in UI code and the performance impact is negligible.
We have a detector which finds these errors (non-constant case
labels referencing an R field) and provides a brief explanation of the problem (and points to this page for more information.) P.S. If your switch statement looks like this:
then you end up with an inefficient
if/else chain where each check repeats the view.getId() call. Just extract thisexpression first (using the "Extract Local Variable" refactoring keystroke), then convert the switch statement. |
Library Project里面使用Case语句判断R.id值报错。case expressions must be constant expressions的更多相关文章
- (转)android import library switch语句报错case expressions must be constant expressions
今天当我从github上下载一个工程,并把它的库文件导入eclipse中,发现switch语句报错case expressions must be constant expressions : 解决方 ...
- 在Android library中不能使用switch-case语句访问资源ID的原因分析及解决方案
转自:http://www.jianshu.com/p/89687f618837 原因分析 当我们在Android依赖库中使用switch-case语句访问资源ID时会报如下图所示的错误,报的错误 ...
- android switch语句报错:case expressions must be constant expressions
今天无意中碰见了 case expressions must be constant expressions 的问题 写了一个 switch(item.getItemId()) { case R. ...
- android switch语句case expressions must be constant expressions
在项目中遇到这样的Exception:case expressions must be constant expressions public class StandingCityActivity e ...
- Android switch 中 case expressions must be constant expressions 错误
刚才导入android zxing 条码 的demo测试,发现出现如下错误 case expressions must be constant expressions 经检查,项目被设置成librar ...
- mysql用查询结果当删除的判断条件进行删除报错1093 You can't specify target table解决方法
mysql用查询结果当删除的判断条件进行删除报错1093 You can't specify target table解决方法 #分开两个sql执行正常的语句,只保留最新1000条数据,删掉1000条 ...
- PL/SQL编写的SQL语句插入SqlPlus时,报错 PLS-00302
最近刚开始用PL/SQL,然后发现写SQL语句时,运行的时候,会对表中的字段报错. 好像是对字段的使用有问题 原来写的错误代码大概像这样 DECLARE xuehao XSB.id% TYPE; BE ...
- case expressions must be constant expressions
As the error message states, the case expressions must be constant. The compiler builds this as a ve ...
- C#中,switch case语句中多个值匹配一个代码块的写法
switch (num) { case 1: Response.Write("1"); break; case 2: case 3: Response.Write("2| ...
随机推荐
- SANBA服务和FTP服务
1.samba服务 Smb主要作为网络通信协议:Smb是基于cs(client/server)架构(架构还有bs,broswer/server):完成linux与windows之间的共享:linux与 ...
- Python3--中括号"[]"与冒号":"在列表中的作用
先来定义两个列表: liststr = ["helloworld","hahahh","123456"] listnum = [1,2,3, ...
- 剑指Offer(书):二进制中1的个数
题目:输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 分析:下面这两种方法都可以,不过第二种更好一些. public int numberOf1(int n) { int count ...
- Java学习笔记(1)-(GridBagLayout)网格袋布局
学习JAVA-布局管理的时候,在书上看到了这么一段话:GridBagLayout的功能非常强大,使用是也比较复杂,考虑到一般的读者很少会使用到这种管理,这里不做介绍.然书本就跳过了,为什么功能强大却很 ...
- ORACLE-023:令人烦恼的 ora-01722 无效数字
https://blog.csdn.net/yysyangyangyangshan/article/details/51762746
- 查看java进程中哪个线程在消耗系统资源
1 top -p $pid -H 加上-H这个参数后,会列出有哪些线程.这样就可以看到哪个线程id最消耗系统资源了.看到的线程id是10进制的数字. 2 jstack $pid 可以打印出制定jav ...
- [vijos1159]岳麓山上打水
[vijos1159]岳麓山上打水 试题描述 今天天气好晴朗,处处好风光,好风光!蝴蝶儿忙啊,蜜蜂也忙,信息组的同学们更加忙.最近,由于XX原因,大家不得不到岳麓山去提水.55555555~,好累啊. ...
- ISAPI映射路径错误,导致K3Cloud打不开。
今天一个同事说她的K3Cloud打不开,一看是页面报500错误,具体信息看图片: 问题: ISAPI配置的映射路径错了,多了个反斜线. 解决办法: 在IIS管理器中找到ISAPI筛选器,删除掉就行了.
- 2018.11.5 PION模拟赛
期望:30 + 40 + 50 = 120 实际:30 + 50 + 40 = 120 ‘’ 思路:最重要的是发现 是完全没有用的,然后这个题目就可以转成DP来做. /* 期望的分:30 */ #in ...
- tomcat的安装和使用
1.下载最新的tomcat:https://tomcat.apache.org/download-90.cgi 最新版本9.0.2属于beta版本,就没有必要尝鲜踩坑了 使用8.5.24的稳定版本的二 ...
