【Java异常】 switch case出现Constant expression required
问题解决依据: switch case支持的类型:byte,short,char,int,枚举类型,java.lang.String
switch (deviceType){
case DeviceTypeConsts.DEVICE_TYPE_PHONE:
// 获取用户id
Integer participantsId = auth.getInteger("participantsId");
// 获取participantsToken
String participantsToken = auth.getString("participantsToken");
return meetingParticipantsService.isLogin(participantsId, participantsToken);
case DeviceTypeConsts.DEVICE_TYPE_PC_ADMIN:
// 获取会员
Integer associatorId = auth.getInteger("associatorId");
//获取token
String associatorToken = auth.getString("associatorToken");
return associatorService.isLogin(associatorId, associatorToken);
case DeviceTypeConsts.DEVICE_TYPE_PROJECTION:
Integer meetingId = auth.getInteger("meetingId");
String meetingIdToken = auth.getString("meetingIdToken");
return meetingService.auth(meetingId, meetingIdToken);
default:
return false;
}
/**
* 设备类型
* @author lhr
* @create 2018/1/9
*/
public class DeviceTypeConsts {
/**
* 手机端
*/
public static final Integer DEVICE_TYPE_PHONE = 0;
/**
* pc后台
*/
public static final Integer DEVICE_TYPE_PC_ADMIN = 1;
/**
* 投影端
*/
public static final Integer DEVICE_TYPE_PROJECTION = 2;
}
这是代码,看着貌似没什么问题因为我们平时很多时候都是对int和Integer混着用,这确实是一个非常不好的习惯,接着我把Integer换成了int
/**
* 设备类型
* @author lhr
* @create 2018/1/9
*/
public class DeviceTypeConsts {
/**
* 手机端
*/
public static final int DEVICE_TYPE_PHONE = 0;
/**
* pc后台
*/
public static final int DEVICE_TYPE_PC_ADMIN = 1;
/**
* 投影端
*/
public static final int DEVICE_TYPE_PROJECTION = 2;
}