android项目变为library项目的问题和解决

来源:互联网 发布:万国数据员工待遇 拉勾 编辑:程序博客网 时间:2024/04/28 23:59


   最近在合并项目的时候,发现一个原来正常的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:
And if you select this option, you get the following 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.

2 0
原创粉丝点击