switch case statement error: case expressions must be constant expression

后端 未结 9 1356
予麋鹿
予麋鹿 2020-12-04 07:51

My switch-case statement works perfectly fine yesterday. But when I run the code earlier this morning eclipse gave me an error underlining the case statements in color red a

相关标签:
9条回答
  • 2020-12-04 08:11

    I would like to mention that, I came across the same situation when I tried adding a library into my project. All of a sudden all switch statements started to show errors!

    Now I tried to remove the library which I added, even then it did not work. how ever "when I cleaned the project" all the errors just went off !

    0 讨论(0)
  • 2020-12-04 08:14

    Simply declare your variable to final

    0 讨论(0)
  • 2020-12-04 08:15

    Unchecking "Is Library" in the project Properties worked for me.

    0 讨论(0)
  • 2020-12-04 08:16

    It was throwing me this error when I using switch in a function with variables declared in my class:

    private void ShowCalendar(final Activity context, Point p, int type) 
    {
        switch (type) {
            case type_cat:
                break;
    
            case type_region:
                break;
    
            case type_city:
                break;
    
            default:
                //sth
                break;
        }
    }
    

    The problem was solved when I declared final to the variables in the start of the class:

    final int type_cat=1, type_region=2, type_city=3;
    
    0 讨论(0)
  • 2020-12-04 08:20

    In a regular Android project, constants in the resource R class are declared like this:

    public static final int main=0x7f030004;
    

    However, as of ADT 14, in a library project, they will be declared like this:

    public static int main=0x7f030004;
    

    In other words, the constants are not final in a library project. Therefore your code would no longer compile.

    The solution for this is simple: Convert the switch statement into an if-else statement.

    public void onClick(View src)
    {
        int id = src.getId();
        if (id == R.id.playbtn){
            checkwificonnection();
        } else if (id == R.id.stopbtn){
            Log.d(TAG, "onClick: stopping srvice");
            Playbutton.setImageResource(R.drawable.playbtn1);
            Playbutton.setVisibility(0); //visible
            Stopbutton.setVisibility(4); //invisible
            stopService(new Intent(RakistaRadio.this,myservice.class));
            clearstatusbar();
            timer.cancel();
            Title.setText(" ");
            Artist.setText(" ");
        } else if (id == R.id.btnmenu){
            openOptionsMenu();
        }
    }
    

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

    You can quickly convert a switch statement to an if-else statement using the following:

    In Eclipse
    Move your cursor to the switch keyword and press Ctrl + 1 then select

    Convert 'switch' to 'if-else'.

    In Android Studio
    Move your cursor to the switch keyword and press Alt + Enter then select

    Replace 'switch' with 'if'.

    0 讨论(0)
  • 2020-12-04 08:23

    Simple solution for this problem is :

    Click on the switch and then press CTL+1, It will change your switch to if-else block statement, and will resolve your problem

    0 讨论(0)
提交回复
热议问题