(转)Android 升级 ADT 之后报错之一 case语句 .
转: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. |
(转)Android 升级 ADT 之后报错之一 case语句 .的更多相关文章
- android升级adt和sdk之后无法识别SDK Location的一个解决方式
我把android的adt和sdk从4.0升级到4.2,发现eclipse的android设置里面原来列出的各种api level的platform消失了,而且无法新建android工程.而且检查过了 ...
- Android 升级ADT到22第三方Jar包导致的ClassNotFoundException和NoClassDefFoundError异常解决
在使用异步载入框架Android-Universal-Image-Loader的Jar包的时候遇到错误: java.lang.NoClassDefFoundError:com.nostra13.uni ...
- 升级Xcode 10 后报错问题记录([CP] Copy Pods Resources)
1.升级Xcode到Version 10.0 (10A255)后,运行已有项目,报如下错误: error: Multiple commands produce '/Users/galahad/Libr ...
- 搭建Android开发环境之——Android4.0.3, 4.1, 4.2, 4.3, 4.x,及升级 ADT(22.0.5)和SDK(22.x)
1.首先要下载相关的软件 1). JDK 6 以上 2). eclipse( Version 3.6.2 or higher ) 点击下载 3). SDK(android-sdk_r18-windo ...
- Android 加了自定义Application后报错 Unable to instantiate activity ComponentInfo ClassNotFoundException
在Android自定义一个类继承集成Application后,并在AndroidManifest.xml里面配置了application的name属性为该类名称后报错: Unable to insta ...
- 【转】Android SDK,ADT,API 版本的对应关系
写对应关系之前,先了解一下几个名字的含义. 一. Android ADT: 按照官方网站的开发介绍:Android Development Tools (ADT) is a plugin for th ...
- 解决MyEclipse中安装或升级ADT之后SDK Target无法显示的问题
故障现象,在MyEclipse里面安装完最新的android sdk和ADT之后,无法新建项目,Build Target为空,显示一直在loading.即如下面图里面显示的,Target Na ...
- 升级openssh编译报错“configure: error: *** working libcrypto not found, check config.log”的解决办法
问题描述 在linux上,欲将OpenSSH_6.4p1编译升级到OpenSSH_8.0p1时,执行了./configure --prefix=/usr --sysconfdir=/etc/ssh - ...
- zabbix安装unixODBC配置完之后报错
zabbix安装unixODBC配置完之后报错 libmysqlclient_16 not defined in file libmysqlclient_r.so.16 分析 我没有使用centos6 ...
随机推荐
- linux安装lolcat实现彩色文字输出信息
[root@localhost ~]# mount /dev/sr0 /media/[root@localhost ~]# rpm -ivh epel-release-latest-7.noarch. ...
- SQL Injection简介
- Linux --赋予普通用户root 权限
Linux的普通用户在安装一些东西的时候或者执行命令的时候,终端始终会提示权限不够,我们会将这个普通用户赋予root权限,但是,和root还是有区别的,因为只能执行root规定好的一些操作命令. 1. ...
- vue 学习二 深入vue双向绑定原理
vue双向绑定原理 请示总体来讲 就是为data的中的每个属性字段添加一个getter/seter属性 以此来追踪数据的变化,而执行这部操作,依赖的就是js的Object.defineProperty ...
- 网络编程之TCP协议怎么使用?
TCP 通信的客户端:向服务器发送连接请求,给服务器发送数据,读取服务器会写的数据 表示客户端的类: java.net.Socket;此类实现客户端套接字.套接字是两台机器间通信的端点 套接字:包含了 ...
- Delphi之TPersistent类 -----ASSIGN
Delphi之TPersistent类 TPersistent类 TPersistent类是由TObject直接派生的.凡是由TPersistent派生的对象都能够进行流操作.因为所有的组件都是由TP ...
- NX二次开发-UFUN查询体的类型为实体还是片体UF_MODL_ask_body_type
NX9+VS2012 #include <uf.h> #include <uf_obj.h> #include <uf_modl.h> #include <u ...
- 虚拟IP---Linux下一个网卡配置多个IP
转:http://blog.csdn.net/turkeyzhou/article/details/16971225 Linux下配置网卡ip别名何谓ip别名?用windows的话说,就是为一个网卡配 ...
- 2019/11/12 CSP模拟赛&&考前小总结
写在前面的总结 离联赛只有几天了,也马上就要回归文化课了. 有点舍不得,感觉自己的水平刚刚有点起色,却又要被抓回文化课教室了,真想在机房再赖几天啊. 像19/11/11那场的简单题,自己还是能敲出一些 ...
- sqlserver 调优(三)
用户数据库质疑状态处理(可能由于机房断电,数据库服务器异常重启后,导致个别数据库状态质疑): --修复数据库(置疑) -- xxxDB 为需要修复的数据库的名称 ALTER DATABASE xxxD ...
