Error in the Android studio constant expression

Source: Internet
Author: User
Tags switch case

Today, with the Zxing Library integrated in as, the following error has occurred:

常量表达式的错误

This error is a problem with switch case and is prompted to replace the If else
In as, we use the Alt+enter (Opt+enter for Mac) shortcut to convert the switch directly to if else, as shown in:

Detailed instructions are available on the Tools Android website, primarily to avoid resource conflicts between multiple libraries

Non-constant Labels

In a regular Android project, constants in the resource R class is declared like this:

publicstaticfinalint main=0x7f030004;

However, as of ADT, in a library project, they'll is declared like this:

publicstaticint main=0x7f030004;

In other words, the constants is not final in a library project. The Simple:when multiple library projects is combined and the actual values of the fields (which must be reason) Unique) could collide. Before ADT, all fields were final, so as a result, all libraries had to has all their resources and associated Java Co De recompiled along with the main project whenever they were used. This is 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 the library P Rojects.

The reason the fields are no longer final was that it means that the library jars can be compiled once and reused directly In the other projects. As well as allowing distributing binary version of the 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 would 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 is constant at compile time (su Ch that values can is directly copied into the. class files).

The solution for this is simple:convert the switch statement to an IF-ELSE statement. Fortunately, this was very easy in Eclipse. Just Place the caret in the Switch keyword, and press Ctrl-1 (or Cmd-1 on Mac):

Shortcut key method for Eclipse

CTRL-1 (or Cmd-1 on Mac)

In the above scenario, it'll turn the switch statement into this:

int id = view.getId();if (id == R.id.button1) {    elseif (id == R.id.button2) {    elseif (id == R.id.button3) {    action3();}

This was 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 explana tion 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 your end up with a 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) and then convert the switch STA Tement.

Original: Non-constant in case Labels

Interested children's shoes can follow my blog, my column will continue to update the Android Studio tutorial, as well as I/O assembly on the NDK configuration and compilation, I will also share to everyone.
And I received an invitation from CSND's instructor, and later I will record the use of these Android studio videos into a video published at Csdn College.

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

Error in the Android studio constant expression

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.