将棋プログラミング

(将棋に関する)ソフトウェア開発のノウハウ等。

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

Androidアプリ開発中に、次のコードを書き、

AlertDialog.Builder ad = new AlertDialog.Builder(getApplicationContext());

次のエラーが発生した。

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

この場合、次のように書く必要があった。

AlertDialog.Builder ad = new AlertDialog.Builder(ActivityName.this);

参考:

stackoverflow.com

qiita.com