Android studio常量表达式的错误

今天在AS上集成Zxing的库,出现了如下的错误:

常量表达式的错误

这个错误是switch case的问题,提示换成if else

在AS中我们使用Alt+Enter(opt+Enter for Mac)快捷键直接将switch转换为if else,如下图所示:

在Tools Android的网站上有详细的说明,主要是避免多个库之间出现资源冲突

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):

下图为Eclipse的快捷键方法

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.

原文:Non-constant Fields in Case Labels


有兴趣的童鞋可以关注我的Blog,我的专栏会持续更新Android Studio 教程,以及2015 I/O大会上的NDK的配置和编译,我也全部会分享给大家。

并且我收到了CSND 的讲师邀请,后期我会把这些Android Studio的使用教程录制成视频发布在CSDN学院。


/**
 * --------------
 * 欢迎转载   |  转载请注明
 * --------------
 * 如果对你有帮助,请点击|顶|
 * --------------
 * 请保持谦逊 | 你会走的更远
 * --------------
 * @author zsl
 * @github https://github.com/yy1300326388
 * @blog http://blog.csdn.net/yy1300326388
 */
时间: 2024-08-01 22:42:00

Android studio常量表达式的错误的相关文章

Android Studio代码自动检测错误提示

Android Studio的代码自动检测的错误提示方式感觉有点奇葩,和Eclipse差别很大,Eclipse检测到某个资源文件找不到或者错误,都会在Project中对应的文件前面打叉,但是Android Studio不用这种方式,所以估计你刚开始找半天找不到错误提示到底在哪?这个错误提示的方式是直接进行了整理归类,不像Eclipse在对应的文件前打叉显示.那如何打开错误提示的列表,如下图: 做个补充,可能很多人会找不到Message在哪,其实你只要选择Build,然后把工程clean或者reb

ubuntu android studio 编译及运行错误Error retrieving parent for item: No resource found that matches the given name

安装好android studio并且安装其它需要的SDK或组件后,根据向导生成新的项目, 编译或运行时可能会出现下面的错误: Error:Error retrieving parent for item: No resource found that matches the given name “xxxxxxxxxx” 不大确定其它人的同样错误是否与本人是同一原因,在本人环境中的问题是“build.gradle(Module:app)”中的配置版本过高的问题. 本人生成项目时,目标andro

Android Studio安装及常见错误

Android Studio 1.0 已经放出来了,以后的Android平台开发激昂逐步从Eclipse向Android Studio迁移,为了能不落伍我也特意从Google下载了Android Studio的安装包,并且兴高采烈地创建了我的第一个android项目.但是当运行的时候就他么悲催了. emulator: ERROR: x86 emulation currently requires hardware acceleration! Please ensure Intel HAXM is

Android Studio运行出现项目 错误:非法字符:“\ufeff”

导入项目时,遇到编码问题. 主要原因是: eclipse可以自动把UTF-8+BOM文件转为普通的UTF-8文件,但Android Studio需要重新转换一下编码 解决方法: 将编码格式UTF-8+BOM文件转为普通的UTF-8文件. 在Android Studio右下角,将编码改为GBK,再转为UTF-8,可以解决. 原文地址:https://www.cnblogs.com/QW-lzm/p/9028628.html

用android studio运行weex项目错误汇总

在本地使用命令weex run android运行weex项目时报错:"Error: No android devices found.",后面发现这是个坑,需要使用android studio启动项目来运行. 在本地下载安装好android studio环境和工具后,将weex项目下platforms->android文件引入(如果是ios则引入ios文件夹),然后编译运行,在编译运行中遇到了如下几个问题,在此汇总记录: 1.Could not find com.android

Android Studio 导入的项目编码错误问题

错误提示: Error:(4, 35) 閿欒: 缂栫爜UTF-8鐨勪笉鍙槧灏勫瓧绗? 解决方法: 1). 2). 的下面添加下面的一行语句: android{compileOptions.encoding="GBK"}

android studio 1.4 开发错误记录 -不断更新

记录开发中遇到的问题,不断更新 1.AVD模拟器启动卡在android界面: 原因:手动设置了internal storage大小. 创建模拟器时internal storage不要修改,否则启动不了:即使启动的了,修改的容积也不会发生变化. 即看配置的时候已经变成例如1G,实际上启动起来到adroid系统中看还是默认的200m. 2.空间不足:Android avd internal storage 不足 默认空间只有200m,实际可用的更少,装几个程序就不够用了. 解决方法:扩容 方法1:命

Android Studio项目生成Jar包

步骤: 1)在module的gradle文件中,将apply plugin:'com.android.application'改为apply plugin:'com.android.library' 2)同样在在module的gradle文件中,将applicationId注释 3)sync一下 4)有时候,会提示错误:常量表达式的错误,一般是switch case问题,快速替换为if else 5)在gradle工具栏选择other->transformClassesAndResourcesW

Android Studio --“Cannot resolve symbol” 解决办法

Android Studio 无法识别同一个 package 里的其他类,将其显示为红色,但是 compile 没有问题.鼠标放上去后显示 "Cannot resolve symbol XXX",重启 Android Studio,重新 sync gradle,Clean build 都没有用. 多半是因为 Android Studio 之前发生了错误,某些 setting 出了问题.解决方法如下: 点击菜单中的 "File" -> "Invalid