Android实现浮窗时报的android.view.WindowManager$BadTokenException: Unable to add window异常

今天在实现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

猜你喜欢

转载自blog.csdn.net/msl0903/article/details/41925821