Kotlin Type mismatch, required: x found: x?
Answer #1 100 %Short answer: Yes.
This means that the compiler is not sure if s.th. is !=null
. If you are sure that it is not null you can also use:
val fragmentActivity = (iMainActivity as Fragment).activity!!
That gives you FragmentActivity
instead of FragmentActivity?
and you dont need the ?.let{}
Keep in mind that that might throw a NPE, while the
fragmentActivity?.let { fragment ->
meViewModel = ViewModelProviders.of(fragment, vmf).get(MeViewModel::class.java)
}
would simply not execute the block within .let{}
, which is often less harmful then a NPE. See https://kotlinlang.org/docs/reference/null-safety.html for more.