Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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) } 
like image 340
humazed Avatar asked Oct 30 '17 13:10

humazed


1 Answers

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.

like image 110
humazed Avatar answered Oct 24 '22 07:10

humazed