本文介绍了Kotlin ArrayAdapter错误:使用提供的参数无法调用以下功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Kotlin的Android开发新手.在执行教程项目时,我需要将ArrayAdapter与自定义类一起使用.构建项目失败,并出现错误.

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.

    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)
        }
    }
}

错误:

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

CourseInfo类:

The CourseInfo Class:

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

DataManager类:

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)
    }
}

推荐答案

非kotlin函数不允许使用命名参数.

问题是因为您要在kotlin文件中调用 java ArrayAdapter 构造函数.您将无法为其提供参数标签.

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())

这篇关于Kotlin ArrayAdapter错误:使用提供的参数无法调用以下功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 18:24