`

AlertDialog显示错误 Unable to add window token null is not for an application

 
阅读更多

在listView的onItemClick函数中显示一个AlertDialog,出现如下错误

08-07 21:26:43.506: ERROR/AndroidRuntime(9390): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

后google解决。

原来代码中显示语句如下

AlertDialog imageDialog = new AlertDialog.Builder(context).setTitle("状态操作").setItems(items, listener).create();
                imageDialog.show();

其中context为在OnCreate中

context = getApplicationContext();

 得到

 

异常原因:AlertDialog创建语句

public AlertDialog.Builder (Context context) 

 中,不能使用getApplicationContext()得到的context,而必须使用Activity,所以解决如下

 

解决方法:语句修改为

AlertDialog imageDialog = new AlertDialog.Builder(Activity.this).setTitle("状态操作").setItems(items, listener).create();
                imageDialog.show();

 其中的Activity为当前Activity的名称

 

参考:

http://blog.csdn.net/yimo29/article/details/6004782

 

 
分享到:
评论
5 楼 Escalade 2017-01-06  
           
4 楼 genggeng007 2014-11-14  
多谢!!!!!!!!!!!!!!
3 楼 ccclll1990 2014-02-26  
非常感谢. !
2 楼 sulanyan29 2012-08-31  
1 楼 124333675 2012-03-07  
哥哥~~~大恩不言谢啊~~~

相关推荐

Global site tag (gtag.js) - Google Analytics