今天在实现Android浮窗时,报了一个异常android.view.WindowManager$BadTokenException: Unable to add window

根据以往的经验,出现这问题一般是我们的Context不正确,但是getApplicationContext、getApplication和this(Service对象)都报错。

在网上搜了一个demo,对比了一下,发现WindowManager.LayoutParams对象的type未设置

代码如下:

wm = (WindowManager) getApplication().getSystemService(Application.WINDOW_SERVICE);LayoutParams lp = new LayoutParams();lp.width = 200;lp.height = 60;lp.type = LayoutParams.TYPE_PHONE;View view = LayoutInflater.from(getApplication()).inflate(R.layout.window_float, null);wm.addView(view, lp);

关于type属性的可参考http://blog.sina.com.cn/s/blog_8e9c63c70101km4h.html

更多相关文章

  1. Android开发开发技巧之 EditText 属性大全
  2. TextView属性
  3. XML属性
  4. Android组件属性
  5. Android属性 gravity, layout_gravity, padding, layout_margin

随机推荐

  1. Android(安卓)数据交互加密
  2. [init.rc] android init.rc 总结
  3. Android层次结构
  4. Android(安卓)RadioButton与ListView的混
  5. android WebView网页浏览器
  6. Android(安卓)SDK/ADT 历史版本下载地址
  7. android shape的使用
  8. android studio 2.2下载地址
  9. 垂直跑马灯、滚动通知
  10. Android头部停留及分页加载功能整合列表