Hel*_*oCW 4 android kotlin android-studio androidx
代码A在我的旧版本中运行良好,现在我将Android Studio更新到3.4.2,并把buildToolsVersion“ 29.0.1”更新了,并且使用了最新的androidx。
但是我收到错误Type不匹配,您可以看到图1,我该如何解决?谢谢!
图片1
代码A
class PreferenceTool<T>(private val context: Context, private val name: String, private val default: T) {
private val prefs: SharedPreferences by lazy {
context.defaultSharedPreferences
}
operator fun getValue(thisRef: Any?, property: KProperty<*>): T = findPreference(name, default)
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
putPreference(name, value)
}
@Suppress("UNCHECKED_CAST")
private fun findPreference(name: String, default: T): T = with(prefs) {
val res: Any = when (default) {
is Long -> getLong(name, default)
is String -> getString(name, default)
is Int -> getInt(name, default)
is Boolean -> getBoolean(name, default)
is Float -> getFloat(name, default)
else -> throw IllegalArgumentException("This type can be saved into Preferences")
}
res as T
}
@SuppressLint("CommitPrefEdits")
private fun putPreference(name: String, value: T) = with(prefs.edit()) {
when (value) {
is Long -> putLong(name, value)
is String -> putString(name, value)
is Int -> putInt(name, value)
is Boolean -> putBoolean(name, value)
is Float -> putFloat(name, value)
else -> throw IllegalArgumentException("This type can't be saved into Preferences")
}.apply()
}
}
Run Code Online (Sandbox Code Playgroud)
新增内容1
而且,我发现代码B和代码C都可以正确编译。我不知道为什么 似乎is String -> getString(name, default)导致错误。
代码B
@Suppress("UNCHECKED_CAST")
private fun findPreference(name: String, default: T): T = with(prefs) {
val res: Any = when (default) {
is Long -> getLong(name, default)
else -> throw IllegalArgumentException("This type can be saved into Preferences")
}
res as T
}
Run Code Online (Sandbox Code Playgroud)
代码C
@Suppress("UNCHECKED_CAST")
private fun findPreference(name: String, default: T): T = with(prefs) {
val res: Any = when (default) {
is Long -> getLong(name, default)
is Int -> getInt(name, default)
is Boolean -> getBoolean(name, default)
is Float -> getFloat(name, default)
else -> throw IllegalArgumentException("This type can be saved into Preferences")
}
res as T
}
Run Code Online (Sandbox Code Playgroud)
新增内容2
似乎由图像2prefs.getString(name, default)返回。我不知道是否有Build 29.0.1的错误吗?String?
图片2
此代码无法编译,因为SharedPreferences.getString()return String?并不是的子类型Any。为什么以前能奏效,这是一个错误吗?
这不是错误,而是android sdk中逐渐向null安全过渡。当kotlin成为android开发的官方语言时,他们开始致力于使android sdk kotlin友好。特别是,他们在sdk代码中添加@Nullable和@NonNull注释。但是立即这样做会破坏许多项目的编译,因此他们决定给开发人员一些时间来修复其代码:
通常,在Kotlin中违反可空性合同会导致编译错误。但是为了确保新注释的API与您现有的代码兼容,我们使用Kotlin编译器团队提供的内部机制将这些API标记为最近注释的API。最近带注释的API仅会导致警告,而不是Kotlin编译器的错误。您将需要使用Kotlin 1.2.60或更高版本。
我们的计划是从次年的Android SDK开始,使新添加的可空性注释仅生成警告,并将严重性级别提高到错误。目的是为您提供足够的时间来更新代码。
正如他们所说,他们将在后续的SDK中提高严重性级别,而这恰恰就是这里发生的情况。但是的源代码SharedPreferences没有更改,那么它是怎么发生的呢?答案是@RecentlyNullable。
(1)这里有一些注释不在支持库中,例如@RecentlyNullable和@RecentlyNonNull。这些仅在存根中用于自动将代码标记为最近用null / non-null注释的代码。我们不希望在源代码中使用这些注释。最近状态是在构建时计算的,并代替常规的null注释注入到存根中。
即使SharedPreferences.getString在@Nullable很久以前就进行了注释,显然他们的自动最近性计算决定将其标记为@RecentlyNullableandroidP。为了证明让我们看一下反编译的代码。
Android P:
@RecentlyNullable
String getString(String var1, @RecentlyNullable String var2);
Run Code Online (Sandbox Code Playgroud)
Android Q:
@Nullable
String getString(String var1, @Nullable String var2);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
178 次 |
| 最近记录: |