switch case报错case expressions must be constant expressions

开发中遇到如此低级问题,实在不该,不过遇到了还是记录一下,具体原因及解决参考该博文:http://blog.csdn.net/woshixuye/article/details/8349731 

一、发现问题

private int CLICK_QUERY = 1;
private int CLICK_RESET = 2;

@Override
public void onClick(View v)
{
int tag = (Integer) v.getTag();
switch (tag)
{
  case CLICK_QUERY:
   query();
  break;
  case CLICK_RESET:
   reset();
  break;
}
}
编译时一直报错:CLICK_QUERY 和CLICK_RESET——case expressions must be constant expressions

二、解决问题

case后面必须跟常量,必须要常量,将上面两个变量声明为final即可。

private final int CLICK_QUERY = 1;
private final int CLICK_RESET = 2;

你可能感兴趣的:(switch case报错case expressions must be constant expressions)