您的位置:首页 > 移动开发

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

2011-12-22 16:30 363 查看
在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
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();
AlertDialog imageDialog = new AlertDialog.Builder(context).setTitle("状态操作").setItems(items, listener).create();
imageDialog.show();

其中context为在OnCreate中

context = getApplicationContext();
context = getApplicationContext();

得到,而AlertDialog创建语句

public AlertDialog.Builder (Context context)
public AlertDialog.Builder (Context context)

中,不能使用getApplicationContext()得到的context,而必须使用Activity,所有语句修改为

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

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

其中的Activity为当前Activity的名称

参考:
/article/1853430.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐