写了这段代码:
import android.widget.ArrayAdapter
//代码块//
val values : Array<String> = arrayOf("USD", "UAH", "GBD", "EUR", "BIT", "RUB")
val adapter = ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, values)
但是,IDE 会引发错误
None of the following functions can be called with the arguments supplied.
<init>(Context, Int, Array<(out) TypeVariable(T)!>)
where T = TypeVariable(T) for
constructor ArrayAdapter<T : Any!>(context: Context, resource: Int, objects: Array<(out) T!>) defined in android.widget.ArrayAdapter
<init>(Context, Int, Int)
where T = TypeVariable(T) for
constructor ArrayAdapter<T : Any!>(context: Context, resource: Int, textViewResourceId: Int) defined in android.widget.ArrayAdapter
<init>(Context, Int, (Mutable)List<TypeVariable(T)!>)
where T = TypeVariable(T) for
constructor ArrayAdapter<T : Any!>(context: Context, resource: Int, objects: (Mutable)List<T!>) defined in android.widget.ArrayAdapter Alt+Shift+Enter Alt+Enter
你能建议这里有什么问题吗?
如果有,则需要将其替换
HomeFragment
为or 。this
requireActivity()
requireContext()
Fragment
与活动不同,它不是从类继承的Context
,这意味着它不能作为上下文传递给参数。以上 2 种方法只是可以在任何内部获取上下文Fragment-а