0
public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case Constants.EXIT :   
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            break;
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

and the Constants class :

public  class Constants {

    private Constants() {
    }

    public static final int EXIT = R.id.mExit;

}

what am i missing ? "menuExitButton " isn't constant ?

Jesus Dimrix
  • 4,378
  • 4
  • 28
  • 62
  • Try to use `if else` in place of `switch` and check if it works. – Apoorv Nov 16 '13 at 11:24
  • possible duplicate of [Java switch statement: Constant expression required, but it IS constant](http://stackoverflow.com/questions/3827393/java-switch-statement-constant-expression-required-but-it-is-constant) – Tobias Nov 16 '13 at 11:24
  • Interesting.. Just wondering why are you not using `R.id.mExit` directly like `case R.id.mExit` ? – Shobhit Puri Nov 16 '13 at 11:27
  • yes . i know i can use if else . i am trying to avoid it and understand why my contants isnt consider as one . – Jesus Dimrix Nov 16 '13 at 12:49

1 Answers1

0

If the R.id.mExit comes from a libary project resource file, it's not a constant expression. Initializing a static final variable with a non-constant expression does not make the variable itself constant.

For further informationa about contant variable initialization, see the answer linked by @still_learning in comments.

Move the resource that declares the identifier to the main app project so it becomes a constant variable that can be used in a switch expression, or just replace the switch with an if-else.

Community
  • 1
  • 1
laalto
  • 150,114
  • 66
  • 286
  • 303