【问题标题】:Kotlin ArrayAdapter Error: None of the following functions can be called with the arguments suppliedKotlin ArrayAdapter 错误:不能使用提供的参数调用以下函数
【发布时间】:2018-10-23 12:14:56
【问题描述】:

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

抛出错误的 MainActivity.kt 类:

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

【问题讨论】:

  • 错误出现在哪一行?

标签: android kotlin android-arrayadapter


【解决方案1】:

也许为时已晚,但也许对某人有所帮助。如果您正在使用 Fragments,则选中的答案将无法解决。您必须像这样调用活动上下文。

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

【讨论】:

    【解决方案2】:

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

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

    所以进行简单的更正即可解决问题。(请注意,我在第一个参数中删除了上下文标签。)

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

    【讨论】:

    • 感谢@Vipul Shah 的解释。错误消失了!
    • 这似乎是真的,但我在任何有关它的文档中都找不到它。你有参考吗?
    【解决方案3】:

    调用 requireContext() 代替上下文参数

    【讨论】:

      【解决方案4】:

      使用下面的代码

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

      【讨论】:

        【解决方案5】:

        val adapters = activity?.let { ArrayAdapter(it.applicationContext, R.layout.support_simple_spinner_dropdown_item, stringArray) } as SpinnerAdapter

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-10-30
          • 1970-01-01
          • 1970-01-01
          • 2018-10-03
          • 2019-06-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多