LoginSignup
22
16

More than 5 years have passed since last update.

Dialogを表示しようとしてWindowManager$BadTokenExceptionが出る場合の対処

Posted at

AlertDialogとかProgressDialogとかをshowしようとして以下の様な例外で落ちることがある。

WindowManager$BadTokenException Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@xxxxxxxx is not valid; is your activity running?

原因1. ContextにActivity由来でないContextを渡している。

Activityを渡してやってください。

× getApplicationContext()
× getBaseContext()
○ MyActivity.this

原因2. ActivityGroupの子から表示しようとしている。

親のActtivityを渡してやってください。

× this
○ getParent()

22
16
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
16