java中使用switch case报错case expressions must be constant expressions

简介:


一、发现问题

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;

 

目录
相关文章
|
12天前
|
Java
Java.Switch case 应用
Java.Switch case 应用
5 0
|
27天前
|
Java
Java switch case 语句
4月更文挑战第11天
|
7月前
|
存储 Java 编译器
【Java异常】Variable used in lambda expression should be final or effectively final
【Java异常】Variable used in lambda expression should be final or effectively final
80 0
【Java异常】Variable used in lambda expression should be final or effectively final
|
11月前
|
Java
Java switch case语句
Java switch case语句
72 0
enum和switch case结合使用
在将enum和switch case结合使用的过程中,遇到了这个错误:“An enum switch case label must be the unqualified name of an enumeration constant”。
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
192 0
|
Dart
Dart之break、continue/ switch...case
Dart之break、continue/ switch...case
78 0
Dart之break、continue/ switch...case
|
Java
Java - Lambda Error:Variable used in lambda expression should be final or effectively final
Java - Lambda Error:Variable used in lambda expression should be final or effectively final
1474 0
Java - Lambda Error:Variable used in lambda expression should be final or effectively final
|
Android开发 Kotlin
【错误记录】Kotlin 编译报错 ( Not nullable value required to call an ‘iterator()‘ method on for-loop range )
【错误记录】Kotlin 编译报错 ( Not nullable value required to call an ‘iterator()‘ method on for-loop range )
223 0
【错误记录】Kotlin 编译报错 ( Not nullable value required to call an ‘iterator()‘ method on for-loop range )
DEFAULT keyword in ABAP and Optional Class in Java
DEFAULT keyword in ABAP and Optional Class in Java
DEFAULT keyword in ABAP and Optional Class in Java