原文地址: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 as
R.id.button1, to be constant at compile time (such that the values can be directly
copied 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
if
check repeats the view.getId() call. Just extract this
expression first (using the "Extract Local Variable" refactoring
keystroke), then convert the switch statement.

Library Project里面使用Case语句判断R.id值报错。case expressions must be constant expressions的更多相关文章

  1. (转)android import library switch语句报错case expressions must be constant expressions

    今天当我从github上下载一个工程,并把它的库文件导入eclipse中,发现switch语句报错case expressions must be constant expressions : 解决方 ...

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

    转自:http://www.jianshu.com/p/89687f618837 原因分析   当我们在Android依赖库中使用switch-case语句访问资源ID时会报如下图所示的错误,报的错误 ...

  3. android switch语句报错:case expressions must be constant expressions

    今天无意中碰见了   case expressions must be constant expressions 的问题 写了一个 switch(item.getItemId()) { case R. ...

  4. android switch语句case expressions must be constant expressions

    在项目中遇到这样的Exception:case expressions must be constant expressions public class StandingCityActivity e ...

  5. Android switch 中 case expressions must be constant expressions 错误

    刚才导入android zxing 条码 的demo测试,发现出现如下错误 case expressions must be constant expressions 经检查,项目被设置成librar ...

  6. mysql用查询结果当删除的判断条件进行删除报错1093 You can't specify target table解决方法

    mysql用查询结果当删除的判断条件进行删除报错1093 You can't specify target table解决方法 #分开两个sql执行正常的语句,只保留最新1000条数据,删掉1000条 ...

  7. PL/SQL编写的SQL语句插入SqlPlus时,报错 PLS-00302

    最近刚开始用PL/SQL,然后发现写SQL语句时,运行的时候,会对表中的字段报错. 好像是对字段的使用有问题 原来写的错误代码大概像这样 DECLARE xuehao XSB.id% TYPE; BE ...

  8. case expressions must be constant expressions

    As the error message states, the case expressions must be constant. The compiler builds this as a ve ...

  9. C#中,switch case语句中多个值匹配一个代码块的写法

    switch (num) { case 1: Response.Write("1"); break; case 2: case 3: Response.Write("2| ...

随机推荐

  1. (15) openssl签署和自签署证书的多种实现方式

    1.采用自定义配置文件的实现方法 1.1 自建CA 自建CA的机制:1.生成私钥:2.创建证书请求:3.使用私钥对证书请求签名. 由于测试环境,所以自建的CA只能是根CA. 所使用的配置文件如下: [ ...

  2. CSS--基础结构层叠

    权值:通配符*的权值为0,标签和伪元素的权值为1,类选择符,属性选择器或伪类的权值为10,ID选择符的权值为100,内联样式最高为1000.还有一个权值比较特殊--继承也有权值但很低,有的文献提出它只 ...

  3. css选择器(2)——属性选择器和基于元素结构关系的选择器

    在有些标记语言中,不能使用类名和id选择器,于是css2引入了属性选择器. 3.属性选择器 a)根据是否存在该属性来选择 如果希望选择有某个属性的元素,例如要选择有class属性的所有h1元素,使其文 ...

  4. springMVC model传对象数组 jq 获取

    这个问题网上没有什么解答,有两种可能性: 一.我使用的这种方法实在太蠢了正常人都不会去这个搞: 二.我太蠢了.... 以下解决方案 //后台代码如下 public String plant(Model ...

  5. 在html借助元素特性存储信息

    背景:比如存在学生选择的CheckBox,希望在CheckBox中同时存储学生的姓名及其所在的城市,比如选择Lily所对应的CheckBox以后,可以获得Lily所在的城市“NewYork”. htm ...

  6. [数据结构]C#基于数组实现泛型顺序表

    前方预警,只完成了顺序表的插入/删除/查找. 错误代码示例: /// <summary> /// 查找顺序表第i个位置的元素 /// 在显示情况中,我们更常用下标 /// </sum ...

  7. SHELL二十篇(读书笔记)

    一.文件安全与权限 1.系统默认情况下建立文件与目录的权限: 系统默认情况下建立文件与目录的权限是: #vi /etc/bashrc 可以看到一般用户是002  root用户是022 说明:一般用户默 ...

  8. 大数据学习——hadoop的RPC框架

    项目结构 服务端代码 test-hadoop-rpc pom.xml <?xml version="1.0" encoding="UTF-8"?> ...

  9. zoj 2850 Beautiful Meadow

    Beautiful Meadow Time Limit: 2 Seconds      Memory Limit: 65536 KB Tom's Meadow Tom has a meadow in ...

  10. COJ 1208 矩阵快速幂DP

    题目大意: f(i) 是一个斐波那契数列 , 求sum(f(i)^k)的总和 由于n极大,所以考虑矩阵快速幂加速 我们要求解最后的sum[n] 首先我们需要思考 sum[n] = sum[n-1] + ...