class Add : Fragment()
{
val types = arrayOf("simple User", "Admin")
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val t=inflater.inflate(R.layout.fragment_add, container, false)
val spinner = t.findViewById<Spinner>(R.id.spinner2)
spinner?.adapter = ArrayAdapter(activity?.applicationContext, R.layout.support_simple_spinner_dropdown_item, types) as SpinnerAdapter
spinner?.onItemSelectedListener = object :AdapterView.OnItemSelectedListener{
override fun onNothingSelected(parent: AdapterView<*>?) {
println("erreur")
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
val type = parent?.getItemAtPosition(position).toString()
Toast.makeText(activity,type, Toast.LENGTH_LONG).show()
println(type)
}
}
return t
}
}
Asked
Active
Viewed 505 times
-3

Alexander Hoffmann
- 5,104
- 3
- 17
- 23

Arijit Sarkar
- 1
- 1
-
Does this answer your question? [Kotlin - Type mismatch: required: Context found: Context?](https://stackoverflow.com/questions/52753000/kotlin-type-mismatch-required-context-found-context) – a_local_nobody Apr 07 '21 at 08:11
2 Answers
0
Its required non-null Context object (Context class) and you give nullable Context object (Context? class). You can manually cast it to non-null with operator !! context!!
, and it will work if context != null
(activity!!.applicationContext!!
in your case). But it is not the best practic, because if it is null, you can get NPE. So better wrap your code with
if (activity!= null && activity.applicationContext != null) {
//your code with context cast (activity!!.applicationContext!!)
}

Yegorf
- 400
- 2
- 11
-
1
-
Yes, it is more laconic solution. but i wanted OP to understand what we actually checking :) – Yegorf Apr 07 '21 at 08:01
-
1If you understand the Fragment lifecycle, it should be fine to use requireContext() in this situation because the context cannot possibly be null in onCreateView(). That is preferable to wrapping code you know should always run in an if statement that suggests that it might not run. In a situation like this, an NPE would be preferable to silently doing nothing because it would alert you to any coding error immediately the first time you run it. – Tenfour04 Apr 07 '21 at 11:37
0
getActivity returns a Activity? and you should get a Activity without null
Use a requireActivity() or activity!!

Andrey Romanyuk
- 31
- 1
- 4
-
As we can see in the Kotlin documentation - Like NPE? Use !!. It is very bad practice, because it destroys the null-safety of Kotlin. – Yegorf Apr 07 '21 at 07:57
-
In debug mode, you should catch an exception to avoid errors in the program code. If you use activity !!, your application will crash and make you think about the correctness of the logic. requireActivity () an equivalent entry with an activity !! – Andrey Romanyuk Apr 07 '21 at 08:03
-
`In debug mode, you should catch an exception to avoid errors in the program code` better yet, just code so you don't have exceptions. don't use unsafe casts – a_local_nobody Apr 07 '21 at 08:10