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

2022-01-19 00:00:00 switch-statement android java

我的 switch-case 语句昨天运行良好.但是当我今天早上早些时候运行代码时,eclipse给了我一个错误,用红色强调了case语句并说:case表达式必须是常量表达式,它是常量我不知道发生了什么.下面是我的代码:

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 and says: case expressions must be constant expression, it is constant I don't know what happened. 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;
        }
    }

所有 R.id.int 都用红色下划线.

All R.id.int are all underlined in red.

推荐答案

在常规的Android项目中,资源R类中的常量声明如下:

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

public static final int main=0x7f030004;

但是,从 ADT 14 开始,在库项目中,它们将被声明为:

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.

解决方案很简单:将 switch 语句转换为 if-else 语句.

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

您可以使用以下命令将 switch 语句快速转换为 if-else 语句:

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

在 Eclipse 中
将光标移动到 switch 关键字并按 Ctrl + 1 然后选择

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

将switch"转换为if-else".

Convert 'switch' to 'if-else'.

在 Android Studio 中
将光标移动到 switch 关键字并按 Alt + Enter 然后选择

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

将开关"替换为如果".

Replace 'switch' with 'if'.

相关文章