Library Project里面使用Case语句判断R.id值报错。case expressions must be constant expressions
2015-07-23 15:48
274 查看
原文地址: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 switchstatement 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 switchstatement 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.) More information about the automatic detection. P.S. If your switch statement looks like this: then you end up with an inefficient if/elsechain where each ifcheck repeats the view.getId() call. Just extract this expression first (using the "Extract Local Variable" refactoring keystroke), then convert the switch statement. |
相关文章推荐
- PING的原理以及ICMP协议
- 【SSH异常系列】Cannot call sendError() after the response has been committed
- 学习总结连载二
- 如何配置Eclipse实现定制登录界面
- 图像指定坐标点的各通道像素值提取
- HBase导入数据,数据提交失败
- 数据库连接池 单例模式的实现
- phoneGap for ios 安装环境搭建
- 使用python SimpleHTTPServer 快速搭建Web服务器
- source insight 如何新建一个工程
- Android 权限介绍
- ios 懒加载
- 查找出现次数大于n/k的重复元素
- 读书笔记MoreEffectiveC++(6)
- Qt GUI 通过鼠标事件剖析整个事件处理流程(基于Qt4.5.2源码windows平台)
- HDU 2016
- Android Environment.getExternalStorageState使用
- list.clear()和list=null的区别
- jquery 插件写法笔记
- 关于XShell上登录跳板机问题及第七天实习记录