android - 类型不匹配 : inferred type is String? 但需要字符串 kotlin

标签 android string android-studio kotlin compiler-errors

我收到错误消息:

AddAddressActivity.kt: (69, 53): Type mismatch: inferred type is String? but String was expected

enter image description here

最佳答案

这是因为 getString 方法返回一个可为 null 的值 (String?),但您将其分配给采用非 null 值的内容(预计 字符串)。如果你想修复它,你可以将它分配给默认字符串(如果它为空),如下所示

val villageId = getString("village_id") ?: "default"

或空字符串

val villageId = getString("village_id").orEmpty()

关于android - 类型不匹配 : inferred type is String? 但需要字符串 kotlin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70661753/

相关文章:

java.util.regex.PatternSyntaxException : null (in java. util.regex.Pattern)错误

android - AppCompatActivity 无法正常工作

android-studio - onActivityResult 未在 Fragment 中调用

android - ListView 从自定义 BaseAdapter 中删除项目

android - 无法成功登录 Twitter(使用 Twitter4J 库)重定向回我的 Android 应用

c# - XAMARIN - 从 youtube 添加视频

android - 无法加载 AppCompat ActionBar,出现未知错误

c - C中如何优雅地遍历一个字符串到最后

string - Format polynomial to normal form> (如何让最后的结果在lisp中变成string?(包括所有函数))

google-maps - Android Studio(无法找到 Google API 镜像)在虚拟设备配置中