android - Kotlin ArrayAdapter 错误 : None of the following functions can be called with the arguments supplied

标签 android kotlin android-arrayadapter

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

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

错误:

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 类:

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

DataManager 类:

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

最佳答案

也许为时已晚,但也许对某人有所帮助。如果您使用的是 fragment ,则不会通过已检查的答案来解决。您必须像这样调用 Activity 上下文。

val adapter = activity?.let {
        ArrayAdapter<String>(
            it,
            android.R.layout.simple_spinner_item,
            campaignsType
        )
    }

关于android - Kotlin ArrayAdapter 错误 : None of the following functions can be called with the arguments supplied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52948853/

相关文章:

java - 在具有跨度的 Android TextView 中一次换行两行

android - 如何从其适配器类中获取扩展 ListFragment 的 Activity 的上下文?

java - Android OCR 库不支持

kotlin - Completable.andThen结果为 “other is null”

android - 使自定义主题使用空背景(Android lint 建议)

android - 更新Android Studio后,Android资源链接失败错误

android - SQLite 数据库和 ListView : NullPointerExeption

android如何设置以编程方式准备的arrayAdapter布局

android - 我们可以使用谷歌分析跟踪用户在应用程序中花费的时间吗

java - 如何在 Android 中更改全局变量