Home > front end >  I am getting error on ArrayAdapter saying that function does not exists
I am getting error on ArrayAdapter saying that function does not exists

Time:04-25

class ThirdFragment : Fragment() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    arguments?.let {
    }
}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?,
): View? {
    val view2 = inflater.inflate(R.layout.fragment_third, container, false)

    val a = arrayOf("Java","C","C  ","Python","Kotlin")
    var l1: ListView = view2.findViewById(R.id.list)

    val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, a)
    l1.adapter = adapter
    
    return view2

this is showing on alt enter on "ArrayAdapter"

None of the following functions can be called with the arguments supplied. (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 (Context, Int, Int)   where T = TypeVariable(T) for   constructor ArrayAdapter<T : Any!>(context: Context, resource: Int, textViewResourceId: Int) defined in android.widget.ArrayAdapter (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

CodePudding user response:

The error tells you the first argument to the ArrayAdapter constructor needs to be a Context, so replace this with something that returns a context, like requireActivity().

val adapter = ArrayAdapter(requireActivity(), android.R.layout.simple_list_item_1, a)

In this scope this refers to the fragment, which is not a Context object.

You can break that error message down a bit to make it easier to read by adding some line breaks. It is telling you that there are three valid constructors, with the following signatures, and that the arguments you have supplied do not match any of them.

None of the following functions can be called with the arguments supplied.

 (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 
  
 (Context, Int, Int)   where T = TypeVariable(T) 
  for constructor ArrayAdapter<T : Any!>(context: Context, resource: Int, textViewResourceId: Int) 
  defined in android.widget.ArrayAdapter 
  
 (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
  • Related