I am trying to to create a Spinner
inside a Fragment
but I am getting error in the ArrayAdapter
constructor call. I don\'t know why, b
The problem is in this constructor call:
spinner.adapter = ArrayAdapter(
this,
R.layout.support_simple_spinner_dropdown_item,
resources.getStringArray(R.array.atoms)
) as SpinnerAdapter
The argument this
must be a reference to a Context
, and a Fragment
is not a Context
. In an Activity
it works because an Activity
is a Context
.
The solution is to replace this
with activity
:
spinner.adapter = ArrayAdapter(
activity,
R.layout.support_simple_spinner_dropdown_item,
resources.getStringArray(R.array.atoms)
)