我的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.
推荐答案
在一个普通的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;
在换句话说,常量不是最终的库项目。 因此,你的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();
}
}
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'.
相关推荐
最新文章