switch语句无法使用R.id.xxx的解决办法
来源:互联网 发布:苹果手机炒股软件 编辑:程序博客网 时间:2024/05/22 18:56
下面文章大意是指:在一般的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:
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:
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.)
More information about the automatic detection.
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.http://tools.android.com/tips/non-constant-fields
- switch语句无法使用R.id.xxx的解决办法
- R.id或者R.layout等等在使用时后面无法找找相应空间或者布局问题的解决办法
- switch语句的使用
- switch语句的使用
- switch语句的使用
- switch语句的使用
- 关于R.id.xxx或者R.layout.xxx找不到id或布局文件的解决方案
- Android 中switch case 报错 R.id.xxx没有final
- switch语句的基本使用
- switch 语句的使用范围
- switch case语句的使用
- 关于switch语句的使用
- switch语句的简单使用
- 问题解决:在R.java文件下面找不到自定义的ID(R.id.xxx找不到)
- Your content must have a TabHost whose id attribute is 'android.R.id.tabhost' 问题,写了id语句仍出错的解决办法
- Mybaties 的sql语句无法使用聚合函数的解决办法
- R语言package无法安装的解决办法
- 在Android library中不能使用switch-case语句访问资源ID的原因分析及解决方案
- 面向对象编程和面向接口编程
- Android应用自动更新功能的代码实现
- 使用ant管理工程的HelloWorld示例
- windows bat脚本编写入门,可以做windows的crontab
- 常用的BaseAction类
- switch语句无法使用R.id.xxx的解决办法
- WTL/ATL CString CStringA 到 char *
- struts2 实现原理解析
- zoj 1696 Viva Confetti
- iOS7-解决 "The document “XXXX.xib/storyboard" could not be opened. Could not read archive"
- C++ 获取当前正在执行的函数的相关信息
- 读取保存密码的ini文件
- 关于Smarty插件中truncate截取中文乱码的解决方案
- uva 10701(简单二叉树)