Commutator expressions must be constant expressions

I am using ActionBarSherlock and then onOptionsItemSelected

to start a new action when a specific menu item has been selected. The code worked correctly before adding the ABS and now I am getting the error case expressions must be constant expressions

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));
            break;
        case R.id.feedback: //error
            //launch activity
            break;

        default:
            break;
        return super.onOptionsItemSelected(item);
    }

      

The same code worked great before adding ActionBarSherlock.

+3


source to share


4 answers


I replaced the operator switch/case

with if/else

. You can just click switch

and then click CTRL+1

if you are in Eclipse.



+15


source


Posted as an answer as Sam advised:

If you are in a library, you need to change all switch / case statements so that if / else blocks from ADT version 14.



See:

tools.android.com/tips/non-constant-fields

+12


source


To switch from operator switch

/ case

to if

/ for

, just use alt+ enterin android studio.

+1


source


The end of the switch is missing. "}".

-1


source







All Articles