原文地址: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. 教你轻松在React Native中使用自定义iconfont

    在react-native项目中我们一般使用到 react-native-vector-icons(这里不介绍如何使用react-native-vector-icons按照官方文档即可)但是当reac ...

  2. solr之windws下搭建solr服务

    安装Solr 首先保证已经正确安装了Java 下载Solr,当前最新版6.1.0 Solr各个版本下载地址 Solr从6.0之后需要Java1.8所以如果使用Solr6.0及其以上版本,请确保Java ...

  3. 【练习】reserving.kr 之imageprc write up

    补充:c++ builder 与 windows  API经典范例 #include <vcl.h> #pragma hdrstop #include "Unit1.h" ...

  4. 【MFC】利用MFC写一个计时器小程序

    1整体设计 创建对话框程序,并且设计对话框相关控件如图 相应的ID和对应的成员变量如图: 我的想法是这样的,只读属性的编辑框添加有CString类型的成员变量(如s_hour),在xxxDlg.h里另 ...

  5. 【04】如何确定ruby安装好

        [04]如何确定ruby安装好     命令行里输入 ruby -v 如果正确输出了 ruby 版本号,就OK了       是不是在Windows平台安装的?如果是,先按照楼上说得打开命令行 ...

  6. Couchbase IV(管理与维护)

    Couchbase IV(管理与维护) 管理 常用命令 Command Description server-list List all servers in a cluster server-inf ...

  7. 实现List集合中数据逆序排列

    Collections.reverse(list); 实现list集合逆序排列

  8. HDU3183A Magic Lamp,和NYOJ最大的数一样

    A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  9. [Go]结构体及其方法

    结构体类型可以包含若干字段,每个字段通常都需要有确切的名字和类型.也可以不包含任何字段,这样并不是没有意义的,因为还可以为这些类型关联上一些方法,这里可以把方法看作事函数的特殊版本. 函数事独立的程序 ...

  10. 关闭spring整合kafka时,消费者一直打印kafka日志

    在log4j.properties中添加如下代码 log4j.logger.org.apache.kafka.common.metrics.Metrics=OFF log4j.logger.org.a ...