Multiple errors in my fragments after updating the support library to 27.0.0
Asked Answered
S

2

26

After updating the support library from v-26.1.0 to v-27.0.0 Multiple errors in my fragments.

here is a list of some these errors:

Error: Smart cast to 'Bundle' is impossible, because 'arguments' is a mutable property that could have been changed by this time.

Error: 'onCreateView' overrides nothing

Error: 'onViewCreated' overrides nothing

Error: Type mismatch: inferred type is View? but View was expected

Error: Type mismatch: inferred type is Context? but Context was expected

Error: Type mismatch: inferred type is FragmentActivity? but Context was expected

Error: Type mismatch: inferred type is FragmentActivity? but Context was expected

from android studio's template for empty fragment.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    if (arguments != null) {
        mParam1 = arguments.getString(ARG_PARAM1)
        mParam2 = arguments.getString(ARG_PARAM2)
    }
}

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater!!.inflate(R.layout.fragment_blank, container, false)
}

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
}
Sebastien answered 30/10, 2017 at 13:46 Comment(1)
Please show example code which causes these errors. See minimal reproducible example for tips about creating a new code example.Adigun
S
42

The Root cause of all of these errors is that in support library v-27.0.0 @Nullable and @NonNullannotations have been added.
and since kotlin language is aware of nullability and has a different type for Nullable and NonNull, unlike Java.
without these annotations, the compiler has no way of differentiating between them, and Android studio was trying his best to infer the right type.

TL;DR: change the types to rightly reflect the nullability status.


Error: Smart cast to 'Bundle' is impossible, because 'arguments' is a mutable property that could have been changed by this time.

change arguments.getString(ARG_NAME) ==> arguments?.getString(ARG_NAME) ?: ""


Error: 'onCreateView' overrides nothing

chane:

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

==>

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

Error: 'onViewCreated' overrides nothing

change:

override fun onViewCreated(view: View?, savedInstanceState: Bundle?)

==>

override fun onViewCreated(view: View, savedInstanceState: Bundle?)

Error: Type mismatch: inferred type is Context? but Context was expected

if context is passed as argument to method, just use the quick fix to replace getContext() with getContext()?.let{}
the same applies to the kotlin short version context.

else if is used to call some method replace getContext().someMethod() with getContext()?.someMethod()

the same applies to the kotlin short version context?.someMethod().


Error: Type mismatch: inferred type is FragmentActivity? but Context was expected

use the fix of the previous error.

Sebastien answered 30/10, 2017 at 13:46 Comment(5)
Adding ?.let{} to an fragment's activity does not allow me to use it as a method's argument. The error is Type mismatch: inferred type is FragmentActivity? but Context was expected. Is there another to do this? Furthermore, when I try to access cacheDir for example, I get another one: Smart cast to 'FragmentActivity' is impossible, because 'activity' is a property that has open or custom getter. Still have to find how to resolve this one.Foreignborn
@StéphanePéchard This will solve your problem. #41086796Rigsby
@StéphanePéchard use "requireContext()" if you're in a fragment to get a non-null context - it'll throw an IllegalStateException if the context is null.Polyandry
@Sebastien I am getting this problem in about more than 100 classes so isn't it a big problem?Rodenhouse
errrrrrrrrr it's not just replacing 1 or 2 characters but bunch of them, a lot! and in some case it affects the flow of code. better downgrade to 26. seriously it's annoying. I really made big mistake by using kotlin and android studio i guess.Witching
I
4

For error message:

Type mismatch: inferred type is Bundle? but Bundle was expected

In class: Fragment, method: onCreateView

From

val arguments = SleepQualityFragmentArgs.fromBundle(arguments)

To

val arguments = SleepQualityFragmentArgs.fromBundle(requireArguments())
Illampu answered 24/8, 2021 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.