Exception android.view.WindowManager$BadTokenException: Unable to add window — token android.os.BinderProxy@419abb98 is not valid; is your activity running?
Exception android.view.WindowManager$BadTokenException: Unable to add window — token android.os.BinderProxy@419abb98 is not valid; is your activity running?
android.view.ViewRootImpl.setView (ViewRootImpl.java:720)
android.view.WindowManagerImpl.addView (WindowManagerImpl.java:356)
android.view.WindowManagerImpl.addView (WindowManagerImpl.java:234)
android.view.WindowManagerImpl$CompatModeWrapper.addView (WindowManagerImpl.java:153)
android.view.Window$LocalWindowManager.addView (Window.java:559)
android.app.ActivityThread.handleResumeActivity (ActivityThread.java:2716)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2151)
android.app.ActivityThread.access$700 (ActivityThread.java:140)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1238)
android.os.Handler.dispatchMessage (Handler.java:99)
android.os.Looper.loop (Looper.java:177)
android.app.ActivityThread.main (ActivityThread.java:4947)
java.lang.reflect.Method.invokeNative (Method.java)
java.lang.reflect.Method.invoke (Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1038)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:805)
dalvik.system.NativeStart.main (NativeStart.java)
■ BadTokenException의 원인 ■
이미 종료된 Activity의 context를 인자로 다이얼로그 창을 표시하려고 할 때 발생.
다이얼로그 창을 표시할 Activity가 없기 때문에 안드로이드 런타임이 BadTokenException이라는 예외를 발생시킨다.
■ 해결법 ■
isFinishing() 메소드를 사용하여 Activity의 종료 여부를 확인 후 처리한다.
■ 샘플 코드 ■
if(!MainActivity.this.isFinishing()) {
AlertDialog.builder dialog = new AlertDialog.builder(MainActivity.this);
dialog.setTitle(“알림”);
dialog.setMessage(“테스트 메세지”);
dialog.show();
}