Android实现浮窗时报的android.view.WindowManager$BadTokenException: Unable to add window异常
16lz
2021-01-23
今天在实现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
更多相关文章
- Android开发开发技巧之 EditText 属性大全
- TextView属性
- XML属性
- Android组件属性
- Android属性 gravity, layout_gravity, padding, layout_margin