android - Kotlin ArrayAdapter 错误:不能使用提供的参数调用以下函数
问题描述
我是 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)
数据管理器类:
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)
}
}
解决方案
也许为时已晚,但也许它可以帮助某人。如果您正在使用 Fragments,则选中的答案将无法解决。您必须像这样调用活动上下文。
val adapter = activity?.let {
ArrayAdapter<String>(
it,
android.R.layout.simple_spinner_item,
campaignsType
)
}
推荐阅读
- c# - 在新房间的服务帐户中执行请求时未发现异常
- api - 通过安全令牌进行 API 身份验证的最佳实践(Node.js 服务器和移动应用程序)
- rest-assured - 使用 Rest Assured 获取所有日期
- amazon-web-services - 无法在控制台中细化打包器构建的图像
- android - Android WebView:单独的 Cookie Jar
- python - 重置 pyqt5 小部件的 glContext
- vue.js - VueJS:使用 CDN URL 而不是 NPM 导入库
- javascript - 电子产生异常
- python - python串行读取的CPU负载
- c# - 如何在 Vue 实例中使用 @ViewBag?