简体   繁体   English

switch case 语句错误:case 表达式必须是常量表达式

[英]switch case statement error: case expressions must be constant expression

My switch-case statement works perfectly fine yesterday.我的 switch-case 语句昨天工作得很好。 But when I run the code earlier this morning eclipse gave me an error underlining the case statements in color red and says: case expressions must be constant expression, it is constant I don't know what happened.但是当我今天早上早些时候运行代码时,eclipse 给了我一个错误,用红色强调 case 语句并说:case 表达式必须是常量表达式,它是常量我不知道发生了什么。 Here's my code below:这是我的代码如下:

public void onClick(View src)
    {
        switch(src.getId()) {
        case R.id.playbtn:
            checkwificonnection();
            break;

        case 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(" ");
            break;

        case R.id.btnmenu:
            openOptionsMenu();
            break;
        }
    }

All R.id.int are all underlined in red.所有 R.id.int 都标有红色下划线。

In a regular Android project, constants in the resource R class are declared like this:在常规的 Android 项目中,资源 R 类中的常量声明如下:

public static final int main=0x7f030004;

However, as of ADT 14, in a library project, they will be declared like this:但是,从 ADT 14 开始,在库项目中,它们将像这样声明:

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.解决方案很简单:将 switch 语句转换为 if-else 语句。

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 http://tools.android.com/tips/non-constant-fields

You can quickly convert a switch statement to an if-else statement using the following:您可以使用以下命令快速将switch语句转换为if-else语句:

In Eclipse在日食中
Move your cursor to the switch keyword and press Ctrl + 1 then select将光标移动到switch关键字并按Ctrl + 1然后选择

Convert 'switch' to 'if-else'.将“开关”转换为“if-else”。

In Android Studio在 Android Studio 中
Move your cursor to the switch keyword and press Alt + Enter then select将光标移动到switch关键字并按Alt + Enter然后选择

Replace 'switch' with 'if'.将“开关”替换为“如果”。

在项目属性中取消选中“是库”对我有用。

Solution can be done be this way:解决方法可以是这样:

  1. Just assign the value to Integer只需分配Integer
  2. Make variable to final使变量最终

Example:例子:

public static final int cameraRequestCode = 999;

Hope this will help you.希望这会帮助你。

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单击开关,然后按CTL+1,它会将您的开关更改为 if-else 块语句,并将解决您的问题

R.id.*, since ADT 14 are not more declared as final static int so you can not use in switch case construct. R.id.*,因为 ADT 14 不再被声明为 final static int,所以你不能在 switch case 构造中使用。 You could use if else clause instead.您可以改用 if else 子句。

How about this other solution to keep the nice switch instead of an if-else:这个其他解决方案如何保持漂亮的开关而不是 if-else:

private enum LayoutElement {
    NONE(-1),
    PLAY_BUTTON(R.id.playbtn),
    STOP_BUTTON(R.id.stopbtn),
    MENU_BUTTON(R.id.btnmenu);

    private static class _ {
        static SparseArray<LayoutElement> elements = new SparseArray<LayoutElement>();
    }

    LayoutElement(int id) {
        _.elements.put(id, this);
    }

    public static LayoutElement from(View view) {
        return _.elements.get(view.getId(), NONE);
    }

}

So in your code you can do this:所以在你的代码中你可以这样做:

public void onClick(View src) {
    switch(LayoutElement.from(src)) {
    case PLAY_BUTTTON:
        checkwificonnection();
        break;

    case STOP_BUTTON:
        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(" ");
        break;

    case MENU_BUTTON:
        openOptionsMenu();
        break;
    }
}

Enums are static so this will have very limited impact.枚举是静态的,因此这将产生非常有限的影响。 The only window for concern would be the double lookup involved (first on the internal SparseArray and later on the switch table)唯一需要关注的窗口是涉及的双重查找(首先在内部 SparseArray 上,然后在切换表上)

That said, this enum can also be utilised to fetch the items in a fluent manner, if needed by keeping a reference to the id... but that's a story for some other time.也就是说,如果需要,通过保留对 id 的引用,此枚举也可用于以流畅的方式获取项目……但这是其他时间的故事。

It was throwing me this error when I using switch in a function with variables declared in my class:当我在类中声明变量的函数中使用 switch 时,它抛出了这个错误:

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时,问题就解决了:

final int type_cat=1, type_region=2, type_city=3;

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!突然间所有的 switch 语句开始显示错误!

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 !如何“当我清理项目时”所有错误都消失了!

只需将变量声明为final

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM