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

来源:互联网 发布:c语言 libevent 编辑:程序博客网 时间:2024/04/30 04:15

在创建一个AlertDialog的时候,遇见一个异常:WindowManager$BadTokenException:Unable to add window--token null is not for an application

 导致报这个错的原因在于new AlertDialog.Builder(mcontext),虽然AlertDialog.Builder aBuilder= new Builder(Context context)的参数是 context,但是不能通过getApplicationContext()获取context,必须使用Activity,因为只有一个Activity才能添加一个窗体。即new AlertDialog.Builder(Activity.this)

0 0