Android studio常量運算式的錯誤

來源:互聯網
上載者:User

標籤:android   常量運算式的錯誤   android-studio   zxing   

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

Android studio常量運算式的錯誤

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.