Android studio常量表达式的错误
来源:互联网 发布:淘宝店铺ip地址怎么查 编辑:程序博客网 时间:2024/06/02 04:32
- 1
点击打开链接
今天我是在合并几个工程的时候,出现这个问题:常量表达式的错误
这个错误是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:
- 1
However, as of ADT 14, in a library project, they will be declared like this:
- 1
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:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
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:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
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:
- 1
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学院。
- Android studio常量表达式的错误
- Android studio常量表达式的错误
- Android studio常量表达式的错误
- android studio 常量表达式错误
- 解决 Android Studio 错误:需要常量表达式
- Android Studio 需要常量表达式
- android 需要常量表达式
- Android Studio的相关错误
- Android Studio的常见错误
- Android Studio遇到的错误
- Android Studio 的 gradle 错误
- Error:(149, 22) 错误: 需要常量表达式
- Android Studio的Gradle错误解决方法
- Android Studio的Gradle错误解决方法
- Android Studio的Gradle错误解决方法
- Android Studio的Gradle错误解决方法
- android studio adb错误的解决方法
- Android Studio-R文件错误的解决办法
- Hyperledger Fabric 开发环境搭建
- 【bzoj1083】[SCOI2005]繁忙的都市 kruskal
- UIWebView&WKWebView获取网页高度
- Python语法
- java知识点
- Android studio常量表达式的错误
- Android:ScrollView起始位置不是最顶部
- CI持续集成系统 jira + jenkins + gerrit
- codeforces 754D Fedor and coupons【优先队列+贪心*好题】
- Android AOP之路四 编译时注解详细讲解
- 彻底搞懂四元数
- Leetcode
- centos上部署kubernetes集群
- 堆排序原理及算法实现(Java)