1

I'm a new developer(If I can call myself one) making an alarm app. I think I made other parts of the app but however hard I try, I can't find out how to fix the error. To access instance in AlarmManager.kt, I can't help but make instance companion object. But I guess that makes the problem.... Please could anyone tell me how can I fix this error? Thanks in advance!

kotlin.UninitializedPropertyAccessException: lateinit property instance has not been initialized

↓ SampleApplication.kt

class SampleApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        instance = this
    }

    companion object {
        lateinit var instance: SampleApplication private set
    }
}

↓AlarmManager.kt

object AlarmManager {
    var mService: MusicService? = null
    lateinit var mediaPlayer: MediaPlayer
    lateinit var mView: View
    lateinit var mTimer: Timer
    val tag1 = "alarm1"
    val tag2 = "alarm2"
    private val windowManager: WindowManager by lazy {
        SampleApplication.instance.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    }
jjin
  • 13
  • 1
  • 3

1 Answers1

4

You would need to register SampleApplication in AndroidManifest file. Then only it will executed as an application class

<application
    android:name=".SampleApplication">
Bhargav Thanki
  • 4,924
  • 2
  • 37
  • 43
  • Thank you so much!!!!!!!!!!!!!!! You saved me!!!!!!!!!!!!!!! – jjin Feb 02 '21 at 22:06
  • Sorry, I didn't know there's 'accept'. I clicked on that V sign in the left(I thought up vote is 'accept') – jjin Feb 04 '21 at 06:07
  • Man such a silly mistake I was doing. Thanks. I thought something new came up in compose UI, and I resumed android after 3 years – Jimit Patel Aug 09 '23 at 12:54