![](/img/trans.png)
[英]Lateinit variable is not initialized in TestNG's @BeforeSuite
[英]Lateinit variable not initialized error but got initialized (android)
我知道这听起来很奇怪。 所以在我的 MainActivity 中,我声明了一个对象SongInfo
的数组,如下所示:
class SongInfo {
lateinit var title: String
lateinit var contentId: String
lateinit var filepath: String
}
这就是我在 MainActivity 中声明数组的方式:
lateinit var songInfo: Array<SongInfo?>
所以,现在当我按下一个按钮时,这个按钮的监听器会执行这个:
searchButton.setOnClickListener {
val cursor = contentResolver.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
arrayOf(
"_id",
"_data",
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DURATION
),
"is_music == 1",
null, null
)
songInfo = arrayOfNulls(cursor!!.count)
for (index in songInfo.indices) {
songInfo[index] = SongInfo()
Log.d(tag, "searchButton: $index")
}
adapter = listMusicService.listMusic(cursor)
adapter.notifyDataSetChanged()
}
ListMusicService
类如下所示:
class ListMusicService(val context: Context, private var adapter: ArrayAdapter<Any>) {
private val tag = "MusicShare/ListMusicService"
fun listMusic(cursor: Cursor): ArrayAdapter<Any> {
val activity = MainActivity()
adapter.clear()
val songInfo = arrayOfNulls<SongInfo>(cursor.count)
var duration: Double
var index = 0
while (cursor.moveToNext()) {
duration = ((cursor.getDouble(4) / 600).roundToInt() / 100).toDouble()
adapter.add(cursor.getString(3) + ": " + cursor.getString(2) + " (" + duration + ")")
val filepath = File(cursor.getString(1))
Log.d(tag, "searchMusic: writing songInfo")
songInfo[index] = SongInfo()
songInfo[index]!!.title = filepath.name
songInfo[index]!!.filepath = filepath.absolutePath
activity.songInfo[index]!!.filepath = filepath.absolutePath
songInfo[index]!!.contentId = cursor.getString(0)
index++
}
return adapter
}
}
现在按下searchButton
会导致kotlin.UninitializedPropertyAccessException: lateinit property songInfo has not been initialized
。 但我不明白为什么,因为在searchButton
的侦听器方法中,我正在初始化数组的每个对象。 有谁知道为什么会发生这种情况,也许出了什么问题?
编辑:创建ListMusicService
:
val listMusicService = ListMusicService(this, adapter)
您正在创建MainActivity
的新对象
val activity = MainActivity()
这只是一个空对象,您需要将this
作为MainAcitiy
一个实例MainAcitiy
给listMusic
方法并使用它。
ListMusicService
类已经接受了一个context
对象,所以看起来你正在传递活动的实例(没有关于它的代码),使用MainAcitivty
的现有适配器实例。
// somewhere in main activity
val listMusicService = ListMusicService(this, adapter)
// inside searchButton.setOnClickListener
adapter = listMusicService.listMusic(cursor)
adapter.notifyDataSetChanged()
在列表音乐服务中
fun listMusic(cursor: Cursor): ArrayAdapter<Any> {
adapter.clear()
val songInfo = arrayOfNulls<SongInfo>(cursor.count)
var duration: Double
var index = 0
while (cursor.moveToNext()) {
duration = ((cursor.getDouble(4) / 600).roundToInt() / 100).toDouble()
adapter.add(cursor.getString(3) + ": " + cursor.getString(2) + " (" + duration + ")")
val filepath = File(cursor.getString(1))
Log.d(tag, "searchMusic: writing songInfo")
songInfo[index] = SongInfo()
songInfo[index]!!.title = filepath.name
songInfo[index]!!.filepath = filepath.absolutePath
activity.songInfo[index]!!.filepath = filepath.absolutePath
songInfo[index]!!.contentId = cursor.getString(0)
index++
}
return adapter
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.