Ext.: http://blog.csdn.net/wchinaw/article/details/7325641
The main idea of the following article is that: in a generic Android project, the R class constants are defined with final, but after ADT 14, if in the library project, it will not have the final keyword,
It is estimated that in the new ADT, the resource file becomes a library ...,
In the case of the switch statement, if you use R.id.xxx, you will be prompted with a problem and do not allow a very good amount of it in a scenario statement.
One way that Google provides is to convert it into a if-else statement, which is ctrl+1 at the switch statement and can be replaced with a if-else. Statement.
non-constant Labels
In a regular Android project, constants in the resource R class is declared like this: publicstaticfinalintmain=0x7f030004; However, as of ADT, in aLibraryProject, they'll is declared like this: public static int main=0x7f030004; In other words, the constants is notFinalIn 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 theswitch Statement requires all the case labels, such asR.id.button1 , to is constant at compile time (such the values can directlycopiedInto 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 on the Switch keyword, and press Ctrl-1 (or Cmd-1 on Mac): In the above scenario, it'll turn theswitch 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 (); The typically in UI code and the performance impact are 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 an inefficientif/else Chain where eachif 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. Http://tools.android.com/tips/non-constant-fields |