【Android 很基础】异常出现在show和dimiss dialog

在show或dimiss dilalog时会有可能会抛出以下异常:

show时,

1.android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41ce6798 is not valid; is your activity running?

2-.android.view.WindowLeaked: 某个activity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@427bebc8 that was originally added here

dimiss时,

1.java.lang.IllegalArgumentException: View not attached to window manager,

2.android.view.WindowLeaked: 某个activity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@427bebc8 that was originally added here,

都是因为该dialog依附的activity被finish掉了(主动或被动),导致dialog找不到activity。

例如:异步提交网络数据,在等待返回时切换了activity,而刚好这个页面被finish掉了;在横竖屏切换的时候,activity被重构了。

解决方法:如果不是因为横竖屏切换导致的(因为横竖屏切换,activity会重构,activity.isFinishing()返回的是false),可以在show或者dimiss之前,判断activity.isFinishing()是否成立,否则还要设置android:configChanges="orientation|screenSize|navigation",让activity不重构。

你可能感兴趣的:(【Android,基础】)