Home > Net >  Error while setting text on textView in kotlin
Error while setting text on textView in kotlin

Time:10-27

I am developing a music app, in player Activity when I try to assign a song title, album, and cover image in activity, it returns an error that late init var is not initialized, and when I remove that it works perfectly. Error is while I fetch info from a song using musicList[songPosition].artUri. Here is my 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()

            }
        }
    }
}

The error is given below.

    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) 

Any suggestions.

CodePudding user response:

If you read the exception properly you can identify the issue. You're trying to load the image here inside setLayout() method, before you initialize the array.

.load(musicList[songPosition].artUri)

you're array initialization is written inside the next method call inside playSong()

 musicList = ArrayList()
  • Related