“android.view.WindowManager$BadTokenException: Unable to add window” on buider.show()

前端 未结 10 892
忘掉有多难
忘掉有多难 2020-11-22 10:43

From my main activity, I need to call an inner class and in a method within the class, I need to show AlertDialog. After dismissing it, when the OK

10条回答
  •  故里飘歌
    2020-11-22 11:06

    In my case I refactored code and put the creation of the Dialog in a separate class. I only handed over the clicked View because a View contains a context object already. This led to the same error message although all ran on the MainThread.

    I then switched to handing over the Activity as well and used its context in the dialog creation -> Everything works now.

        fun showDialogToDeletePhoto(baseActivity: BaseActivity, clickedParent: View, deletePhotoClickedListener: DeletePhotoClickedListener) {
            val dialog = AlertDialog.Builder(baseActivity) // <-- here
       .setTitle(baseActivity.getString(R.string.alert_delete_picture_dialog_title))
    ...
    }
    

    I , can't format the code snippet properly, sorry :(

提交回复
热议问题