Library Project里面使用Case语句判断R.id值报错。case expressions must be constant expressions

原文地址: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 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:

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.

时间: 2024-08-29 03:39:06

Library Project里面使用Case语句判断R.id值报错。case expressions must be constant expressions的相关文章

在Android library中不能使用switch-case语句访问资源ID的原因分析及解决方案

转:http://www.jianshu.com/p/89687f618837 原因分析   当我们在Android依赖库中使用switch-case语句访问资源ID时会报如下图所示的错误,报的错误是case分支后面跟的参数必须是常数,换句话说出现这个问题的原因是Android library中生成的R.java中的资源ID不是常数: 打开library中的R.java,发现确实如此,每一个资源ID都没有被声明为final: 但是当你打开你的主工程,在onClick.onItemClick等各种

android switch语句case expressions must be constant expressions

在项目中遇到这样的Exception:case expressions must be constant expressions public class StandingCityActivity extends Activity implements View.OnClickListener{} @Override public void onClick(View v) { switch (v.getId()) { case R.id.back: break; default: break;

Switch Case语句中多个值匹配同一个代码块的写法

switch ($p) { case 'home': case '': $current_home = 'current'; break; case 'users.online': case 'users.location': case 'users.featured': case 'users.new': case 'users.browse': case 'users.search': case 'users.staff': $current_users = 'current'; break

Android 库工程 提示case expressions must be constant expressions 错误

问题描述:  在库工程中存在如下代码: 代码示例 int id = view.getId(); switch (id) { case R.id.button1: action1(); break; case R.id.button2: action2(); break; case R.id.button3: action3(); break; } 会提示case expressions must be constant expressions 错误 问题原因: ADT14后Android库工程中

Mac上安装R引用rJava报错

最近想学习一下,R昨天安装一个R和Rstudio,安装完成后,本来想找个处理excel的练习练习,于是找了一个xlsx包,发后一直报错,如下图: 困扰了一天,还是没有解决,终于上网找到解决方法,所以记录下,希望对其他出现同样问题的有用. 解决方法:在Mac终端执行命令:  sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib 然后在Rstudio中执行    library("xlsx&qu

pip install –r ./requirements.txt 报错 改成 pip install -r requirements.txt 成功

Invalid requirement: '–r'Traceback (most recent call last): File "/home/dev/.pyenv/versions/3.6.1/envs/env361/lib/python3.6/site-packages/pip/_vendor/packaging/requirements.py", line 92, in __init__ req = REQUIREMENT.parseString(requirement_stri

requirejs r.js 打包报错paths fallback not supported in optimizer please provide a build config path override for jquery

错误原因: 改为:

linux 执行:pip3 install -r requirements.txt 报错

错误内容: 解决办法: 原文地址:https://www.cnblogs.com/guo2733/p/11512360.html

(转)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语句,即