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| ...
随机推荐
- 【笔记】mysql入门语句8条
1.连接到数据库服务器 mysql -h host -uroot -pXXXX 2.查看所有库 show databases; 3.选库 use 库名 4.查看库下面的表 show tables; 5 ...
- 《C/C++工程师综合练习卷》
前言 前天拿这个<C/C++工程师综合练习卷>练习了一下,现将错题以及精题分析总结. 错题分析与总结 2 . 下面的程序可以从1-.n中随机等概率的输出m个不重复的数.这里我们假设n远大于 ...
- .NET Core使用log4Net记录日志
1.引入Nuget包 log4net 2.添加log4Net配置文件 <?xml version="1.0" encoding="utf-8" ?> ...
- Fidder详解-抓取HTTPS清求(Web/App)抓包分析(靠谱篇)
为什么要学Fidder抓包? 学习接口,必须要学http协议,不要求您对协议的掌握有多深.只是希望你能够了解什么是协议.协议的报文.状态码等等!本文通过抓包工具Fidder带你进入接口的大门.我们通过 ...
- POJ1061青蛙的约会
Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事 ...
- hdu 3371
#include<stdio.h> #include<stdlib.h> #define N 501 struct node { int x,y,dis; }road[N*N] ...
- msp430项目编程24
msp430中项目---MMC接口 1.串行通信工作原理 2.串行通信协议 3.代码(显示部分) 4.代码(功能实现) 5.项目总结 msp430项目编程 msp430入门学习
- msp430项目编程04
msp430中项目---TFT彩屏显示 1.TFT彩屏工作原理 2.电路原理说明 3.代码(静态显示) 4.代码(动态显示) 5.项目总结 msp430项目编程 msp430入门学习
- vagrant的学习 之 ThinkPHP3.2
vagrant的学习 之 ThinkPHP3.2 (1)在web目录下新建tp32目录: cd /home/www/ mkdir tp32 (2)下载框架 我从ThinkPHP官网下载了ThinkPH ...
- poj2767,单向连通图判定,缩点+重新建图+新图DFS
/*该题被博客里标记为中等题,30分钟内1A,掌握了算法就简单了,单向连通图判定,单向连通图缩点 后必然唯一存在出度为0的点和入度为0的点,并且从入度为0的点出发,可以遍历所有点后到达出度为0点 (一 ...