简体   繁体   中英

Warning: 'onCreateView' always returns non-null type

Today I found this warning in all my fragments:

Warning:(45, 12) 'onCreateView' always returns non-null type

onCreateView:

override fun onCreateView(inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?): View? {
    ...
}

The question is, what has changed and why?

Android Studio version - 4.1.1.

this is Kotlin warning, probably new lint check for helping you producing better quality code

your method returns declared View? , but as we know in most cases this method shouldn't return null View , so this ? (null safety) is redundant. just remove ? , declare your method returns just View (not null by default in Kotlin)

override fun onCreateView(inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?): View { // in here no "?"
    ...
}

btw. DOC is saying that this method may return null and Fragment behaves then just like some objects/data holder, without UI, just for proper retaining instance across Activity s lifecycle. but in that case dev won't be overriding onCreateView at all

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM