【已解决】android.view.WindowManager$BadTokenException: Unable to add window
来源:互联网 发布:archlinux vim python 编辑:程序博客网 时间:2024/05/31 15:18
private void showCustomDialog() {
// TODO Auto-generated method stub
AlertDialog.Builder builder;
AlertDialog dialog;
LayoutInflater inflator = (LayoutInflater) LayoutInflatorActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflator.inflate(R.layout.dialoglayout, null);
TextView text = (TextView) view.findViewById(R.id.textview);
ImageButton imageButton = (ImageButton) view.findViewById(R.id.imageButton);
builder = new AlertDialog.Builder(this);
builder.setView(view);
dialog = builder.create();
dialog.show();
}
在这句上报错了。因为本来我的写法是:
builder = new AlertDialog.Builder(this.getApplicationContext());
因为看了API 是 new AlertDialog.Builder(Context context);想着也没什么语法错误吧。
结果网上搜了一下,获取上下文this.getApplicationContext()); 和 this的区别:
这里的this指的当然就是Acitivity.this , 指的是这个Acitivity的上下文,而this.getApplicationContext()指的则是整个应用的上下文。
对于AlertDialog来说,是需要依赖一个View,而View是对应于Activity的。
那么为什么会报错呢,这里涉及到一个生命周期的问题了。
对于一个应用Context来说,它的生命周期是整个应用程序的生命周期,而对于Activity来说,当它销毁之后它的生命周期就结束了。
AlertDialog是属于Acitivity的,当Activity销毁的时候它也必须销毁,所以这里我们指定是Activity的Context。
- 【已解决】android.view.WindowManager$BadTokenException: Unable to add window
- 【已解决】android.view.WindowManager$BadTokenException: Unable to add window
- 解决android.view.WindowManager$BadTokenException: Unable to add window -- token .
- android.view.WindowManager$BadTokenException: Unable to add window
- android.view.WindowManager$BadTokenException: Unable to add window
- android.view.WindowManager$BadTokenException: Unable to add window
- android.view.WindowManager$BadTokenException: Unable to add window -- token nul
- 异常android.view.WindowManager$BadTokenException: Unable to add window解决方法
- android.view.WindowManager$BadTokenException: Unable to add window
- android.view.WindowManager$BadTokenException: Unable to add window异常
- android.view.WindowManager$BadTokenException: Unable to add window -- token
- android.view.WindowManager$BadTokenException: Unable to add window
- android.view.WindowManager$BadTokenException: Unable to add window(转)
- android.view.WindowManager$BadTokenException: Unable to add window
- android.view.WindowManager$BadTokenException: Unable to add window
- android.view.WindowManager$BadTokenException: Unable to add window
- android.view.WindowManager$BadTokenException: Unable to add window --Unabletoaddwindow--tokenandroid
- android.view.WindowManager$BadTokenException: Unable to add window
- &在.properties和xml文件中差别,导致数据库中文乱码.
- CTF-Internetwache2016-exp-RemotePrinter
- variably modified 'a' at file scope 之减治法---假币问题
- Windows 7 64位用STC ISP下载51单片机程序的方法
- hdu 2544
- 【已解决】android.view.WindowManager$BadTokenException: Unable to add window
- pycharm 运行spark
- 实现apk的更新
- 时间管理
- 2017.4.22 hankson的趣味题 思考记录
- java 关键字static
- 【GDSOI2017第三轮模拟】Travel Plan(DP)
- leetcode120
- Split()拆分字符串与StringTokenizer拆分字符串