AlertDialog 获取上下文的问题
来源:互联网 发布:unity3d网络游戏实战 编辑:程序博客网 时间:2024/06/06 16:55
Log Error android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an app
今天给删除收货地址写AlertDialog的时候 ,无论怎么样 都是 不能Create ,我是把AlertDialog 写在Adapter里面了 而且上下文mContext已经传过来了 ,后来百度了。
解决办法 在Activity new Adapter 传上下文的时候用 Activity.this 作为上下文 不要用 getApplication() 作为上下文 这样就可以了
如果你把AlerDialog写在Activity上 也要用 Activity.this 作为上下文。。。。。
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);builder.setTitle("提示信息");builder.setMessage("您确定取删除收货地址吗?");builder.setIcon(R.drawable.ic_launcher);builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { delAddress(mDatas.get(position).getAddressId()); }});builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { }});Dialog alertDialog = builder.create();alertDialog.show();
http://blog.csdn.net/yimo29/article/details/6004782
在Activity中newSpinner是我把mContext传入,但是出了Android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application这个错误,参考了达人的文章终于搞定。
导致报这个错是在于new AlertDialog.Builder(mcontext),虽然这里的参数是AlertDialog.Builder(Context context)但我们不能使用getApplicationContext()获得的Context,而必须使用Activity,因为只有一个Activity才能添加一个窗体。
解决方法:将new AlertDialog.Builder(Context context)中的参数用Activity.this(Activity是你的Activity的名称)来填充就可以正确的创建一个Dialog了。
- AlertDialog 获取上下文的问题
- 获取全局的上下文
- 关于JSP中获取全局上下文不能识别的问题
- 获取 DoradoContext的上下文对象:
- 获取上下文信息的方法
- android AlertDialog 的位置问题
- AlertDialog.Builder()的参数问题
- 自定义 AlertDialog 出现的问题
- 关于AlertDialog位置的问题
- AlertDialog的一些相关问题
- 获取canvas上下文报错问题
- Android AlertDialog 获取PositiveButton的控制权
- Android AlertDialog 获取PositiveButton的控制权
- Android自定义AlertDialog的控件获取操作
- Dialog传上下文的问题
- android小问题:如何在Listview中获取上下文菜单对应的item项
- Spring 的上下文Context对象获取
- 怎么获取Android应用程序的上下文
- i.MX6DL学习记录---kernel移植
- Android View事件机制 21问21答
- 剑指offer——最小的K个数
- 杭电 1251 统计难题
- ABAP dump 发邮件
- AlertDialog 获取上下文的问题
- php魔术方法
- linuxOS权限问题,导致php对文件操作报错
- NOI2016 D类老年选手滚粗记
- C# 控制台捕获关闭事件的代码
- iOS 把UILabel的text设置成斜体
- 代码适配Masonry使用的详细介绍
- sublime里调整ecshop里dwt,lbi文件高亮显示代码
- 求素数 sdut oj 1468(欧拉筛)