今天我在所有 fragment 中都发现了这个警告:
Warning:(45, 12) 'onCreateView' always returns non-null type
创建 View :
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
...
}
问题是,发生了什么变化,为什么?Android Studio 版本 - 4.1.1。
最佳答案
这是Kotlin
警告,可能是新的 lint 检查,可帮助您生成更高质量的代码
您的方法返回声明的 View?
,但正如我们所知,在大多数情况下,此方法不应返回 null
View
, 所以这个 ?
(零安全)是多余的。只需删除 ?
, 声明你的方法只返回 View
(在 Kotlin 中默认不为空)
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View { // in here no "?"
...
}
顺便提一句。 DOC是说这个方法可能返回null
和 Fragment
然后就像一些对象/数据持有者一样,没有 UI,只是为了在 Activity
中正确保留实例的生命周期。但在这种情况下 dev 不会覆盖 onCreateView
根本
关于android - 警告 : 'onCreateView' always returns non-null type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65075905/