Kotlin无效检查错误
问题描述:
我使用Java库。在异常类中有“localizedMessage”字段和“描述”字段。我不知道为什么,但是“description”(它是java String)被识别为String!通过Kotlin,当“description”为null时,条件描述== null返回false。的代码 示例:Kotlin无效检查错误
mvpView?.showToast(it.description?:it.localizedMessage)
或不埃尔维斯:
if (it.description.isNullOrBlank()) {
mvpView?.showToast(it.localizedMessage)
} else {
mvpView?.showToast(it.description)
}
它总是试图显示“说明”,但“评估表达”在调试模式下对描述返回true == NULL(如预期)。
科特林版本1.1.1
答
的原因是在吸气剂,它返回另一个字符串,而不是描述,如果描述为空。
public String getDescription() {
if (description != null) {
return description;
}
if (UNKNOWN_ERROR.equals(getCode())) {
return String.format("Received error with code %s", getCode());
}
return "Failed with unknown error";
}
+0
这个异常来自哪里?实际的'java.lang.Exception'没有'description'属性。 (你可以接受你自己的答案。) –
+0
@EugenPechanec来自第三方库 –
你怎么知道它显示'description'而不是'localizedMessage'?也许他们都是空的。 – glee8e
'String!'是用来表示String来自Java的符号,因此Kotlin实际上并不知道它是否可为空。 – pdpi
@ glee8e来自调试器 –