Android(安卓)使用弹出对话框,报Unable to add window错误
16lz
2021-01-24
今天在使用Android弹出对话框的时候,报了一个unable to add window错误,我的代码如下
new AlertDialog.Builder(getApplicationContext()).setTitle("提示").setMessage("你确定要删除么?").setPositiveButton("确定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {} }}).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }).show();
仔细看代码,一切都很正常,也没找到什么错误,但是我们忽略了只有Activity才能添加window,所以上面的代码如果放在onCreate()方法中,我们把 Builder(getApplicationContext())改成Builder(this)即可,如果我们是在其他方法中使用,就不能使用getApplicationContext(),而应该使用Builder(Activity.this)
转载于:https://www.cnblogs.com/yuanking/p/4334688.html
更多相关文章
- Android(安卓)O使用通知遇到的坑
- Android菜单定制总结
- Android踩坑记Caused by: com.android.tools.r8.utils.AbortExce
- 纯代码实现 Android(安卓)登陆布局.
- 拍照-----------android系统 至关重要的功能
- Android(安卓)EditText OnTouchListener事件重复
- ListView下拉加载一(分页)
- Android程序全屏方法
- Android(安卓)webView自适应屏幕