android项目变为library项目的问题和解决
最近在合并项目的时候,发现一个原来正常的android项目变为library项目后,出现了错误提示:
case
expressions must be constant expressions
error on case
.
@Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item)
{
switch (item.getItemId()) {
case R.id.about: //error
startActivity(new Intent(this, AboutActivity.class));
解决:
I‘ve replaced the switch/case
statement
with if/else
.
You can just click on switch
and
then press CTRL+1
if
you‘re in Eclipse.
http://stackoverflow.com/questions/14858328/switch-case-expressions-must-be-constant-expressions
Take a look at the official blog post about this:
http://tools.android.com/recent/switchstatementconversion
http://tools.android.com/tips/non-constant-fields
Basically, resource constants in library projects are no longer "final". From the ADT Site:
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.
so if you the the fix it will convert switch into if and else...
int id = view.getId();
if (id == R.id.button1) {
action1();
} else if (id == R.id.button2) {
action2();
} else if (id == R.id.button3) {
action3();
参考:
http://stackoverflow.com/questions/17849566/switch-case-expressions-must-be-constant-expressions-i-cant-make-the-if-else
http://tools.android.com/recent/switchstatementconversion
Switch Statement Conversion
posted Sep 29, 2011, 2:35 PM by Tor Norbye
As of ADT 14, resource constants in library projects are no longer final. This is explained in greater detail in this document.
However, one consequence of this is that some existing projects no longer compile when you use ADT 14. And the reason may be hard to understand. To help with this, there‘s a new quickfix detector which looks for a specific compiler error and when present
adds a "quickfix". This means that when you hover over the error it offers more help:
If you select this quickfix you get this dialog:
(Note - the URL has changed - it‘s now http://tools.android.com/tips/non-constant-fields.)
Hopefully this will make it much more obvious what‘s going on, and make it a lot less painful to update any code which depended on constants in library project resources.
|
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。