今天在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 */

更多相关文章

  1. Android(安卓)Studio TV开发教程(十)添加引导步骤
  2. Android(安卓)Studio快捷键(自用)
  3. RxJava2 使用详解一之基础教程
  4. Opencv4.1链接so错误(android sdk) error: undefined reference t
  5. 视频教程-MongoDB数据库从入门到精通-Mongo DB
  6. 常见Android(安卓)Native崩溃及错误原因
  7. Android(安卓)Debug Bridge 和Drozer安装使用教程
  8. jQuery Mobile 入门教程
  9. Android(安卓)Studio安装教程及第一个HelloWorld程序

随机推荐

  1. Android 自定义RadioButton或CheckBox选
  2. android studio 3.1.1 创建项目编译不过
  3. 可能是最详细的Android点击事件处理详解(
  4. 13-4-4 android的SQLite功能应用
  5. Android(安卓)selector中设置 android:st
  6. android 选择器selector的用法说明
  7. Android 保存数据到文件
  8. Android(java)学习笔记113:Android编写代码
  9. Android群英传第五章笔记·Android Scrol
  10. android中showSoftInput不起作用