Android Studio 3.0-alpha8 中的 Kotlin apply() 扩展 lint 消息 [英] Kotlin apply() extension lint message in Android Studio 3.0-alpha8
问题描述
我有以下代码会产生以下 lint 错误.
fun newInstance(message: String?): DialogFragment {返回 DialogFragment().apply {参数 = Bundle().apply {putString("arg", 消息)}}}
该消息指出 apply()
函数中的 this
引用指向自 API 21 起可用的 BaseBundle
类,该类将在较低的 API.
引用 Bundle
类型而不是 BaseBundle
.
为什么我们首先会出现 Lint 错误?
一种解决方法是使用 let
而不是 apply
,例如:
fun newInstance(message: String?): DialogFragment {返回 DialogFragment().apply {参数 = Bundle().let {it.putString("arg", 消息)它}}}
I have following code that produces following lint error.
fun newInstance(message: String?): DialogFragment {
return DialogFragment().apply {
arguments = Bundle().apply {
putString("arg", message)
}
}
}
The message points out that this
reference inside apply()
function points to BaseBundle
class that is available since API 21 which will crash on lower API. Bundle#putString(key, value) is definitely available on lower versions, but there is an error in Android Studio 3.0-alpha8.
The issue quite strange as I can see decompiled code as this:
Which do reference Bundle
type not a BaseBundle
.
Why do we have Lint error in first place?
One workaround is to use let
instead of apply
, like:
fun newInstance(message: String?): DialogFragment {
return DialogFragment().apply {
arguments = Bundle().let {
it.putString("arg", message)
it
}
}
}
这篇关于Android Studio 3.0-alpha8 中的 Kotlin apply() 扩展 lint 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!