原文地址: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. (4) openssl rsa/pkey(查看私钥、从私钥中提取公钥、查看公钥)

    openssl  rsa      是RSA对称密钥的处理工具 openssl  pkey   是通用非对称密钥处理工具,它们用法基本一致,所以只举例说明openssl rsa. 它们的用法很简单,基 ...

  2. spring boot学习02【如何在spring boot项目中访问jsp】

    1.配置application.properties文件 打开application.properties追加 spring.mvc.view.prefix=/WEB-ROOT/ spring.mvc ...

  3. python024 Python3 实例

    Python3 实例 以下实例在 Python3.4.3 版本下测试通过: Python Hello World 实例 Python 数字求和 Python 平方根 Python 二次方程 Pytho ...

  4. Centos6.5安装Oracle11.2.0.4 RAC(完整版)

    环境参数:Linux:Centos6.5 Grid和Oracle:11.2.0.4 一.环境配置 1.配置Node1和Node2两个节点之间的网卡 Node1: [root@rac1 network- ...

  5. UITableView点击背景

    系统自定义的点击背景有时间觉得效果不好想换个 - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelect ...

  6. CodeForces - 592D Super M 题解

    题目大意: 一棵树 n个点 有m个点被标记 求经过所有被标记的点的最短路径的长度以及起点(如有多条输出编号最小的起点). 思路: 1.当且仅当一个点本身或其子树中有点被标记时该点在最短的路径上因此,可 ...

  7. 病毒的侵扰和再侵扰两道AC自动机的应用

    HDU2896 病毒的侵扰 http://vjudge.net/problem/viewProblem.action?id=16404 题目大意: 记录每个病毒的编号,并给出一些网站的源码,分别输出网 ...

  8. POJ3621 Sightseeing Cows【最短路】

    题目大意:在一个无向图里找一个环,是的点权和除以边权和最大 思路:UVA11090姊妹题 事实上当这题点权和都为1时就是上一题TUT #include <stdio.h> #include ...

  9. 巴蜀4384 -- 【模拟试题】作诗(Poetize)

    Description 神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗.由于时间紧迫,SHY作完诗之后还要虐OI,于是SHY找来一篇长度为N的文章,阅读M次, ...

  10. linux 报错:E: Package 'libmemcached' has no installation candidate

    linux 报错:E: Package 'libmemcached' has no installation candidate 网上查资料说是软件安装源没有这个软件,需要添加软件源. 1.备份源列表 ...