开关case语句错误:案件前pressions必须是恒定的前pression [英] switch case statement error: case expressions must be constant expression

查看:139
本文介绍了开关case语句错误:案件前pressions必须是恒定的前pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的switch-case语句的工作完全没有昨天。但是,当我运行code今天上午早些时候蚀给了我一个错误,强调色彩的case语句红说:遇前pressions必须是恒定的前pression,这是不变的,我不知道发生了什么。下面是我下面的code:

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.

推荐答案

在一个普通的Andr​​oid项目,在资源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;

在换句话说,常量不是最终的库项目。 因此,你的code将不再进行编译。

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();
    }
}

<一个href="http://tools.android.com/tips/non-constant-fields">http://tools.android.com/tips/non-constant-fields

您可以使用快速转换开关语句的的if-else 语句如下:

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

在Eclipse
将光标移动到开关关键字和preSS <大骨节病>控制 + <大骨节病> 1 然后选择

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

转换'开关'到'的if-else'。

Convert 'switch' to 'if-else'.

在Android的工作室
将光标移动到开关关键字和preSS <大骨节病>替代 + <大骨节病>输入然后选择

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

替换'开关'与'如果'。

Replace 'switch' with 'if'.

这篇关于开关case语句错误:案件前pressions必须是恒定的前pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆