繁体   English   中英

Lateinit 变量未初始化错误但已初始化(android)

[英]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一个实例MainAcitiylistMusic方法并使用它。


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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM