转:http://blog.csdn.net/wchinaw/article/details/7325641

下面文章大意是指:在一般的Android项目中,R类的常量都是用final定义的,但ADT 14之后,如果在library 项目中,它会没有final关键字,

估计在新ADT中,资源文件会变成一个library...,

在switch语句的case中,如果使用 R.id.xxx 则会提示有问题,不允许非常量在case语句中。

Google提供的一个方法就是把它转化为if-else语句,即在switch语句处 Ctrl+1 然后可以替换成if-else.语句。

Non-constant Fields in Case Labels

In a regular Android project, constants in the resource R class are declared like this:

publicstaticfinalintmain=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:

switch (view.getId()) {
 
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.
 

最新文章

  1. MySQL安装与设置
  2. CSS布局技巧 -- sticky属性
  3. AWS re:Invent 2014回顾
  4. Java程序设计 实验三
  5. Unity3D Android手机开发环境配置,可真机发布调试
  6. K-集合 (JXNU第二次周赛1006)set/平衡树
  7. Residual Networks <2015 ICCV, ImageNet 图像分类Top1>
  8. Nuget找不到服务器
  9. java 字符串为空问题
  10. uva 111 History Grading(最长公共子序列)
  11. vagrant打造自己的开发环境
  12. java.lang.IllegalStateException: attempt to re-open an already-closed object
  13. 各大型邮箱smtp服务器及端口收集
  14. ngClass指令3种使用
  15. Android中不能在子线程中更新View视图的原因
  16. 【PAT】B1074 宇宙无敌加法器(20 分)
  17. 简话Angular 05 Angular表单验证
  18. 拥抱.NET Core系列:MemoryCache 初识 (转载)
  19. String StringBuffer stringbuilder 区别
  20. Code Forces 652C Foe Pairs

热门文章

  1. linux 查看服务器序列号(S/N)
  2. qt创建无qt工程
  3. CSIC_716_20191128【多态、绑定与非绑定方法、isinstance与issubclass 】
  4. Redis探索之路(四):Redis的五种数据类型Set和ZSet
  5. CDH 下线节点
  6. Web API 接口参考
  7. 如何打造7*24h持续交付通道?阿里高级技术专家的5点思考
  8. sql 生成javabean实体
  9. 使用Windbg调试内核
  10. ionic:安装