Решение состоит в том, чтобы добавить activity?.applicationContext
в адаптер, чтобы изменить ActivityFragment в контекст:
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
}
}