java.lang.IllegalArgumentException:View = DecorView @ ff5fb5d [MainActivity]未附加到窗口管理器

时间:2018-12-26 13:34:06

标签: android kotlin alertdialog

我正在开发一个新应用,当我使用电子邮件和密码登录时,它崩溃了,并显示以下错误消息

  

java.lang.IllegalArgumentException:   View = DecorView @ ff5fb5d [MainActivity]未附加到窗口管理器           在android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:479)           在android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:388)           在android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:128)           在android.app.Dialog.dismissDialog(Dialog.java:610)           在android.app.Dialog.dismiss(Dialog.java:593)           在com.empowered.healo.ui.views.CustomAlertDialog.access $ dismiss $ s-1404601997(CustomAlertDialog.kt:23)           在com.empowered.healo.ui.views.CustomAlertDialog $ dismiss $$ inlined $ let $ lambda $ 1.onAnimationEnd(CustomAlertDialog.kt:107)           在android.animation.ValueAnimator.endAnimation(ValueAnimator.java:1149)           在android.animation.ValueAnimator.doAnimationFrame(ValueAnimator.java:1309)           在android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:146)           在android.animation.AnimationHandler.-wrap2(AnimationHandler.java)           在android.animation.AnimationHandler $ 1.doFrame(AnimationHandler.java:54)           在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:925)           在android.view.Choreographer.doCallbacks(Choreographer.java:702)           在android.view.Choreographer.doFrame(Choreographer.java:635)           在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:913)           在android.os.Handler.handleCallback(Handler.java:751)           在android.os.Handler.dispatchMessage(Handler.java:95)           在android.os.Looper.loop(Looper.java:154)   android.app.ActivityThread.main(ActivityThread.java:6682)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1520)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

我已点击此链接

View not attached to window manager crash

在我的CustomAlertDialog.kt下

class CustomAlertDialog(context: Context) : AlertDialog(context, R.style.AppTheme_FullScreenDialogStyle), CustomAlertDialogBuilder.CustomDialogInterface {
    var dismissListener: CustomAlertDialogBuilder.OnDismissListener? = null
    private var cancelableDialog = true
    var alertDialogInfo = CustomAlertDialogBuilder.AlertDialogInfo()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.custom_dialog)
        if (cancelableDialog) {
            rootDialogLayout.setOnClickListener { dismiss() }
        }

        btnPositive.visibility = if (alertDialogInfo.positiveText != null) View.VISIBLE else View.GONE
        btnPositive.text = alertDialogInfo.positiveText

        btnNegative.visibility = if (alertDialogInfo.negativeText != null) View.VISIBLE else View.GONE
        btnNegative.text = alertDialogInfo.negativeText

        btnNeutral.visibility = if (alertDialogInfo.neutralText != null) View.VISIBLE else View.GONE
        btnNeutral.text = alertDialogInfo.neutralText

        dialogTitle.visibility = if (alertDialogInfo.titleText != null) View.VISIBLE else View.GONE
        dialogTitle.text = alertDialogInfo.titleText

        alertDialogInfo.textColor?.let {
            btnPositive.setTextColor(it)
            btnNegative.setTextColor(it)
            btnNeutral.setTextColor(it)
            message?.setTextColor(it)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                buttonsControl?.dividerDrawable?.colorFilter = PorterDuffColorFilter(it, PorterDuff.Mode.MULTIPLY)
            }
        }

        message?.text = this.alertDialogInfo.message

        alertDialogInfo.background?.let {
            backgroundImage.setImageResource(it)
        }
        btnPositive.setOnClickListener { alertDialogInfo.onPositiveClickListener?.onClick(this) }
        btnNeutral.setOnClickListener { alertDialogInfo.onNeutralClickListener?.onClick(this) }
        btnNegative.setOnClickListener { alertDialogInfo.onNegativeClickListener?.onClick(this) }
    }

    override fun setCancelable(flag: Boolean) {
        super.setCancelable(flag)
        cancelableDialog = flag
    }


    override fun onStart() {
        super.onStart()
        val animationSet = AnimatorSet()
        val decorView = this
                .window
                .decorView
        decorView.setBackgroundColor(Color.TRANSPARENT)
        backgroundImage?.let {
            val titleAnimation = ObjectAnimator.ofFloat(dialogTitle, View.TRANSLATION_Y, -50.dp.toFloat(), 0f)
            val imageAnimation = ObjectAnimator.ofFloat(it, View.SCALE_X, 0f, 1f)
            val startAnimation = ObjectAnimator.ofFloat(decorView, View.ALPHA, 0f, 1f)
            animationSet.playTogether(titleAnimation, imageAnimation, startAnimation)
        }

        animationSet.duration = 350
        animationSet.interpolator = LinearInterpolator()
        animationSet.start()

    }

    override fun dismiss() {
        val animationSet = AnimatorSet()
        val decorView = this
                .window
                .decorView

        backgroundImage?.let {
            val imageAnimation = ObjectAnimator.ofFloat(it, View.SCALE_X, 1f, 0f)
            val titleAnimation = ObjectAnimator.ofFloat(dialogTitle, View.TRANSLATION_Y, 0f, -50.dp.toFloat())
            val endAnimation = ObjectAnimator.ofFloat(decorView, View.ALPHA, 1.0f, 0.0f)

            val listeners = object : AnimationListener() {
                override fun onAnimationEnd(animation: Animator?) {
                    super@CustomAlertDialog.dismiss()
                    dismissListener?.onDismiss()
                }
            }
            imageAnimation.addListener(listeners)
            animationSet.playTogether(titleAnimation, endAnimation, imageAnimation)
        }

        animationSet.duration = 350
        animationSet.interpolator = LinearInterpolator()
        animationSet.start()
    }

    override fun dismiss(callDismissListener: Boolean) {
        if (!callDismissListener)
            setOnDismissListener { }
        dismiss()
    }

    override fun dismiss(onDismissListener: CustomAlertDialogBuilder.OnDismissListener) {
        dismissListener = onDismissListener
        dismiss()
    } }

0 个答案:

没有答案