Java 为什么android studio告诉我enum.valueOf总是正确的?

Java 为什么android studio告诉我enum.valueOf总是正确的?,java,android,android-studio,enums,null,Java,Android,Android Studio,Enums,Null,我有个奇怪的问题。这是我的枚举: enum ErrorInByte{ ERROR_BIT0(3), ERROR_BIT2(4), ERROR_BIT3(5), ERROR_BIT4(7), ERROR_BIT5(13), ERROR_BIT7(15), private int value; ErrorInByte(int value) { this.value = value; } public

我有个奇怪的问题。这是我的枚举:

enum ErrorInByte{
    ERROR_BIT0(3),
    ERROR_BIT2(4),
    ERROR_BIT3(5),
    ERROR_BIT4(7),
    ERROR_BIT5(13),
    ERROR_BIT7(15),

    private int value;
    ErrorInByte(int value) {
        this.value = value;
    }

    public static ErrorInByte valueOf(int value){
        return intToErrorInByte.get(value);
    }

    private static final Map<Integer, ErrorInByte> intToErrorInByte= new HashMap<>();
    static {
        for (ErrorInByte type : ErrorInByte.values()) {
            intToErrorInByte.put(type.value, type);
        }
    }
}
为什么android studio告诉我ErrorInByte.valueOf(n)总是正确的?我测试了它,对于
ErrorInByte.valueOf(326)
它等于null

警告信息:

This inspection analyzes method control and data flow to report possible conditions that are always true or false, expressions whose value is statically proven to be constant, and situations that can lead to nullability contract violations.


Variables, method parameters and return values marked as @Nullable or @NotNull are treated as nullable (or not-null, respectively) and used during the analysis to check nullability contracts, e.g. report possible NullPointerException errors.

More complex contracts can be defined using @Contract annotation, for example:

@Contract("_, null -> null") — method returns null if its second argument is null 
@Contract("_, null -> null; _, !null -> !null") — method returns null if its second argument is null and not-null otherwise 
@Contract("true -> fail") — a typical assertFalse method which throws an exception if true is passed to it 

The inspection can be configured to use custom @Nullable
    @NotNull annotations (by default the ones from annotations.jar will be used)

有没有办法消除警告?我讨厌警告…

发生的情况是
enum
已经有一个不可重写的
valueOf
方法。这意味着您实际上正在调用自己定义的
valueOf
,但IDE假定它是静态的
valueOf
。因此,为了解决您的问题,您必须将您的方法重命名为

 public static ErrorInByte lookUpByCode(int value){
        return intToErrorInByte.get(value);
    }

因为您在地图中没有值为326的元素。请确保值存在或不存在您的问题,并且您的解释没有任何意义。首先你说它总是空的,然后你说它总是真的……你能发布你的警告信息吗?@Georgi这可能对你有帮助,我没想到。改了名字,现在没事了。。。谢谢
 public static ErrorInByte lookUpByCode(int value){
        return intToErrorInByte.get(value);
    }