Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin ArrayAdapter Error: None of the following functions can be called with the arguments supplied

I am new to Kotlin for Android development. While following along a tutorial project, I needed to use ArrayAdapter with a custom class. Building the project failed with error.

The MainActivity.kt class throwing the error:

    class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
    }

    val dm = DataManager()
    val adapterCourses = ArrayAdapter<CourseInfo>(
        context = this,
        android.R.layout.simple_spinner_item,
        dm.courses.values.toList()
    )

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        return when (item.itemId) {
            R.id.action_settings -> true
            else -> super.onOptionsItemSelected(item)
        }
    }
}

Error:

None of the following functions can be called with the arguments supplied:
public constructor ArrayAdapter<T : Any!>(@RecentlyNonNull p0: Context!, p1: Int) defined in android.widget.ArrayAdapter
public constructor ArrayAdapter<T : Any!>(@RecentlyNonNull p0: Context!, p1: Int, @RecentlyNonNull p2: Array<(out) CourseInfo!>!) defined in android.widget.ArrayAdapter
public constructor ArrayAdapter<T : Any!>(@RecentlyNonNull p0: Context!, p1: Int, p2: Int) defined in android.widget.ArrayAdapter
public constructor ArrayAdapter<T : Any!>(@RecentlyNonNull p0: Context!, p1: Int, p2: Int, @RecentlyNonNull p3: Array<(out) CourseInfo!>!) defined in android.widget.ArrayAdapter
public constructor ArrayAdapter<T : Any!>(@RecentlyNonNull p0: Context!, p1: Int, p2: Int, @RecentlyNonNull p3: (Mutable)List<CourseInfo!>!) defined in android.widget.ArrayAdapter
public constructor ArrayAdapter<T : Any!>(@RecentlyNonNull p0: Context!, p1: Int, @RecentlyNonNull p2: (Mutable)List<CourseInfo!>!) defined in android.widget.ArrayAdapter

The CourseInfo Class:

class CourseInfo (val courseId: String, val title: String)

The DataManager Class:

class DataManager {
    val courses = HashMap<String, CourseInfo>()
    val notes = ArrayList<NoteInfo>()

    init {
        initializeCourses()
    }

    private fun initializeCourses () {
        var course = CourseInfo(courseId = "android_intent", title = "Android programming with intent.")
        this.courses.set(course.courseId, course)

        course = CourseInfo(courseId = "android_async", title = "Android Async Programming and Services.")
        courses.set(course.courseId, course)

        course = CourseInfo(courseId = "java_lang", title = "Java Fundamentals: The Java Language.")
        courses.set(course.courseId, course)

        course = CourseInfo(courseId = "java_core", title = "Java Fundamentals: The Core Platforms.")
        courses.set(course.courseId, course)
    }
}
like image 732
Michael Akinyemi Avatar asked Oct 23 '18 12:10

Michael Akinyemi


3 Answers

Maybe too late but maybe it helps someone. If you are working with Fragments it will not be resolved by the checked answer. You must call the activity context like this.

val adapter = activity?.let {
        ArrayAdapter<String>(
            it,
            android.R.layout.simple_spinner_item,
            campaignsType
        )
    }
like image 192
Achref ArShavin Avatar answered Sep 22 '22 18:09

Achref ArShavin


Named arguments are not allowed for non-kotlin functions.

The problem is since you are invoking java ArrayAdapter constructor in kotlin file. you won't be able to provide argument label to it.

so following simple correction would resolve the issue.(Notice here, I removed context label in first argument.)

val adapterCourses = ArrayAdapter<CourseInfo>(this,
            android.R.layout.simple_spinner_item,
            dm.courses.values.toList())
like image 20
Vipul Avatar answered Sep 22 '22 18:09

Vipul


call requireContext() in place of context parameter

like image 28
mahmood Avatar answered Sep 24 '22 18:09

mahmood