debugging 在kotlin中的textView上设置文本时出错

ljo96ir5  于 2022-11-14  发布在  Kotlin
关注(0)|答案(1)|浏览(154)

我正在player Activity中开发一个音乐应用程序,当我尝试在Activity中分配歌曲标题、专辑和封面图片时,它返回一个错误,即未初始化最新的init var,当我删除它时,它工作正常。错误是当我使用musicList[songPosition]. artUri从歌曲中获取信息时发生的。下面是我的player Activity。

import android.media.MediaPlayer
import android.os.Bundle
import android.widget.ImageButton
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.google.android.material.imageview.ShapeableImageView

class musicInterface : AppCompatActivity() {

    lateinit var musicList: ArrayList<MusicClass>
    var songPosition: Int = 0
    var musicPlayer: MediaPlayer? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_music_interface)

        val button: ImageButton = findViewById(R.id.backButton)
        button.setOnClickListener {
            onBackPressed()
        }

        setLayout()
        playSong()
    }

    private fun setLayout() {
        val image: ShapeableImageView = findViewById(R.id.interfaceCover)
        val title: TextView = findViewById(R.id.interfaceSongName)
        val album: TextView = findViewById(R.id.interfaceArtistName)

        Glide
            .with(this)
            .load(musicList[songPosition].artUri)
            .apply(RequestOptions.placeholderOf(R.drawable.image_as_cover).centerCrop())
            .into(image)

        title.text = musicList[songPosition].title
        album.text = musicList[songPosition].artist

    }

    private fun playSong() {
        songPosition = intent.getIntExtra("index", 0)
        when (intent.getStringExtra("class")) {
            "MusicAdapter" -> {
                musicList = ArrayList()
                musicList.addAll(MainActivity.songList)
                if (musicPlayer == null) musicPlayer = MediaPlayer()
                musicPlayer!!.reset()
                musicPlayer!!.setDataSource(musicList[songPosition].path)
                musicPlayer!!.prepare()
                musicPlayer!!.start()

            }
        }
    }
}

错误如下所示。

Process: com.example.music, PID: 25937
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.music/com.example.music.musicInterface}: kotlin.UninitializedPropertyAccessException: lateinit property musicList has not been initialized
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3420)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3559)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2092)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:264)
        at android.app.ActivityThread.main(ActivityThread.java:7593)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)
     Caused by: kotlin.UninitializedPropertyAccessException: lateinit property musicList has not been initialized
        at com.example.music.musicInterface.getMusicList(musicInterface.kt:14)
        at com.example.music.musicInterface.setLayout(musicInterface.kt:42)
        at com.example.music.musicInterface.onCreate(musicInterface.kt:27)
        at android.app.Activity.performCreate(Activity.java:7805)
        at android.app.Activity.performCreate(Activity.java:7794)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3390)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3559) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2092) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:264) 
        at android.app.ActivityThread.main(ActivityThread.java:7593) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980) 

任何建议。

f4t66c6m

f4t66c6m1#

如果你正确地阅读了这个异常,你就可以发现这个问题。你试图在初始化数组之前,在setLayout()方法中加载图像。

.load(musicList[songPosition].artUri)

数组初始化是在playSong()的下一个方法调用中编写的

musicList = ArrayList()

相关问题