문제

While running monkey tool on my app, I am getting android.view.WindowLeaked exception, I referred some stackoverflow threads and found that we need avoid this while creating popups, instead we need to use getApplicationContext() but this is causing BadTokenException

E/AndroidRuntime(5597): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

Again after searching through I found this answer which says

One cause of this error may be trying to display an application window/dialog through a Context that is not an Activity

So how to solve this?

도움이 되었습니까?

해결책

Usually window leaks because You don't use showDialog method in Activity but only using Dialog.show() method.

When dialog is displayed and configuration of Your Activity changes (eg. orientation change) dialog has not window to attach, and this results in leak of window.

If You use Compatibility library You should use DialogFragment instead of pure dialog.

다른 팁

the reason behind WindowManager$BadTokenException is that you keep showing dialog or popupwindow on the window whose context is not alive now.

So should always dismiss popup or dialog whenever you switch between activities or dismiss any activity.

Therefore you should probably call dilog.dismiss() in onPause method.

onPause()
{
dilog.dismiss()
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top