今天在使用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

更多相关文章

  1. Android(安卓)O使用通知遇到的坑
  2. Android菜单定制总结
  3. Android踩坑记Caused by: com.android.tools.r8.utils.AbortExce
  4. 纯代码实现 Android(安卓)登陆布局.
  5. 拍照-----------android系统 至关重要的功能
  6. Android(安卓)EditText OnTouchListener事件重复
  7. ListView下拉加载一(分页)
  8. Android程序全屏方法
  9. Android(安卓)webView自适应屏幕

随机推荐

  1. android studio 中报Error:Execution fai
  2. Android(安卓)6.0指纹识别App开发Demo
  3. android HandlerThread源码解析
  4. Android(安卓)Camera 修改预览分辨率Prev
  5. 深入init进程(and5.1)
  6. Android(安卓)getevent/sendevent详解
  7. android打开系统相册的一些方法
  8. android文件选择器filePicker
  9. android device 注册、添加、管理
  10. (笔记)RecyclerView item按下变色效果 没