Search code examples
androidkotlinprogress-barprogressdialog

Caused by: kotlin.UninitializedPropertyAccessException: lateinit property dialog has not been initialized


I just started learning Kotlin. I am using a custom ProgressDialog. Everytime I press back from the MainActivity, the app crashes with the following error:

  Caused by: kotlin.UninitializedPropertyAccessException: lateinit property dialog has not been initialized
    at com.devApps.blog.CustomProgressDialog.getDialog(CustomProgressDialog.kt:17)

Here is my CustomProgressDialog :

  private val progressDialognew = CustomProgressDialog()
  lateinit var dialog: CustomDialog

fun show(context: Context): Dialog {
    return show(context, null)
}

fun show(context: Context, title: CharSequence?): Dialog {
    val inflater = (context as Activity).layoutInflater
    val view = inflater.inflate(R.layout.progress_dialog_view, null)
    if (title != null) {
        view.cp_title.text = title
    }
    dialog = CustomDialog(context)
    dialog.setContentView(view)
    dialog.show()
    return dialog
}

And this is my MainActivity code :

 private val progressDialognew = CustomProgressDialog()
 progressDialognew.show(this, "Optimizing Image...")
 {
    my tasks here
 }

  override fun onDestroy() {
    super.onDestroy()
    progressDialognew.dialog.dismiss()
}

UPDATE : I did as suggested now I am getting the same Error in the show() at the return dialog line. How to fix that ?

 lateinit var dialog: CustomDialog


fun show(context: Context): Dialog {
    return show(context, null)
}

fun show(context: Context, title: CharSequence?): Dialog {
    if (::dialog.isInitialized) {
        val inflater = (context as Activity).layoutInflater
        val view = inflater.inflate(R.layout.progress_dialog_view, null)
        if (title != null) {
            view.cp_title.text = title
        }

        dialog = CustomDialog(context)
        dialog.setContentView(view)
        dialog.show()
       
    }
    return dialog
}

   
}

fun hideProgress() {

    if (::dialog.isInitialized) {
        if (dialog != null) {
            dialog.dismiss()
        }

    }

Complete StackTrace :

 kotlin.UninitializedPropertyAccessException: lateinit property dialog has not been initialized
    at com.devApps.blog.CustomProgressDialog.show(CustomProgressDialog.kt:50)
    at com.devApps.blog.PostActivity.upload(PostActivity.kt:328)
    at com.devApps.blog.PostActivity.post(PostActivity.kt:320)
    at com.devApps.blog.PostActivity.access$post(PostActivity.kt:54)
    at com.devApps.blog.PostActivity$onCreate$5$2.onClick(PostActivity.kt:198)

Solution

  • If you close the activity without ever showing the dialog, this exception will happen. Because the dialog property in your CustomProgressDialog gets initialized only in show() method.

    And onDestroy() method of the activity calls dismiss() on the dialog property that might not be initialized.

    You can check if it's initialized first before dismissing it, consider adding this method inside CustomProgressDialog:

    fun dismissDialog() {
        if (::dialog.isInitialized) {
            dialog.dismiss()
        }
    }
    

    And call it from the activity:

    override fun onDestroy() {
        super.onDestroy()
        progressDialognew.dismissDialog()
    }