Mismatch Type: Derived Type - FragmentActivity? but when updating the support library to 27.0.0

I updated the Kotlin project to use the support library 27.0.0. When I try to get the Activity context in the Fragment, I get this error:

> Type mismatch: inferred type is FragmentActivity? but Context was expected
+4
source share
1 answer

It is safe to assume that activityeither is contextnot zero in any of the fragment life cycle methods (between onAttachand onDetach)). In this case

context!!

it seems better because if it is null, when it definitely should NOT be empty, something is terribly wrong and you should just crash.

In asynchronous callbacks, of course, check for null.

For me "!!" there will never be an option.

, , . , BaseFragment:

val context: Context
    @JvmName("getContext2") 
    get() = getContext()!!

:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    context // Context is *never* null here.
}

fun someCallbackMethod() {
    if (isAdded) {
        context // Context is also not null here.
    } else {
        // But it is null here.
    }
}

- , null-null assert.

, 27, . context , , null .

+5

Source: https://habr.com/ru/post/1688454/


All Articles