PopWindow在Android 2.3.3 或以下的系统的一个bug 及其解决办法
来源:互联网 发布:ubuntu torch7 安装 编辑:程序博客网 时间:2024/06/09 20:49
今日终于修复了一个非常严重的bug:
这个bug非常奇怪,我在Anroid.4.0.4或以上测试都正常,但是后来廉温说他在他的手机按一下右下角的"设置"按钮(Button),居然出现了崩溃现象(理论上会在设置按钮上弹出一popuwindow);
廉温手机系统2.3.X ;然后我用平板(系统也是2.3.x)测试下,果然出现错误;
由于平板基本报废,无法USB连接地电脑跟踪测试;一开始我没放在心上,因为几台4.0.4or以上系统都测试能正常。。
于是开了模拟器(系统2.3.3)测试,结果还是崩溃,看log日志 ,报了一个NullPointExcetion,这。。实在太费解了!
问题入口:
......
protected PopupWindow window;
......
this.window = new PopupWindow(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
....
这里我实例化一个PopupWindow ,当时我没有注意,以为传入2个MeasureSpec ( Width/Height)也就是窗体layout_height 和layout_weight ; 可以是 FILL_PARENT or WRAP_CONTENT or 具体的高和宽!
悲剧的是,看回文档,居然是:
public PopupWindow (int width, int height)
Added in API level 1
Create a new empty, non focusable popup window. The dimension of the window must be passed to this constructor.
The popup does not provide any background. This should be handled by the content view.
Parameters
这实际上要是指定了content view 的高、宽 而不是MeasureSpec (FILL_PARENT or WRAP_CONTENT 。。)
那就奇怪了,由于ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT 这两个常量是负数
窗体的高宽是负数,按道理是不会显示窗体的啊!而且4.0.4系统还是会个跟2.3.3报同一个错误啊!
但为啥在系统是4.0.4的时候还是正常显示,而2.3.3却错误?
于是我翻看了源码,跟踪记录如下:
Step 1:
249
public PopupWindow(int width, int height) {
250
this(null, width, height);
251
}
Step 2:
265
public PopupWindow(View contentView, int width, int height) {
266
this(contentView, width, height, false); // ->contentView = null
267
}
Step 3:
281public PopupWindow(View contentView, int width, int height, boolean focusable) {282if (contentView != null) {283mContext = contentView.getContext();284mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);285}286setContentView(contentView); // -> 在这里报了个NullPointExcetion ,再跟踪
287setWidth(width);
288setHeight(height);
289setFocusable(focusable);
290}
Step 4 :
2.3.3系统下
376public void setContentView(View contentView) {377if (isShowing()) {
378return;
379}380381mContentView = contentView;382383if (mContext == null) {
384mContext = mContentView.getContext();385}386387if (mWindowManager == null) {388mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);389}390}同是Step 4 :4.0.4 系统下390public void setContentView(View contentView) {391if (isShowing()) {
392return;
393}394395mContentView = contentView;396397if (mContext == null && mContentView != null) {
398mContext = mContentView.getContext();399}400401if (mWindowManager == null && mContentView != null) {402mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);403}404}果然~出现了非常神奇的一幕~ 原来在2.3.3的时候忘记检查mContentView 是否为空,因为mContextView 这个对象可以在实例化一个PopupWindow 后再通过SetContextView(View view)传入
也就是说,这是算是一个系统级的小bug~ 所以在2.3.3 或以前的版本时 就会崩溃,在4.0.4 以后的都不会崩溃;这不能完全怪谷歌,谁让我没有认真看下文档呢?
这也是我第一次发现谷歌的代码漏洞XD解决2.3.3或以下的办法就是用1、可以用PopupWindow(Context context)这个构造方法实例化2、mPopupWindow.setContentView(View view) 传入content view3、mPopupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);mPopupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);这里的setWidth() setHeight() 才是是改变PopupWindow 的Width/Height MeasureSpec转载请说明出处,鼓励原创!
- PopWindow在Android 2.3.3 或以下的系统的一个bug 及其解决办法
- SWFaddress2.3在IE6或IE7下的一个bug
- VS2013自定义向导工程的一个bug及其解决办法
- IE8及以下JavaScript的一个bug
- VS2008的2个BUG及其解决办法
- SQLITE在ANDROID上的一个BUG
- android popwindow的使用
- android popwindow的使用
- Android 6.0 的popwindow
- android 5.0以下系统Intent传递序列化对象的bug
- 如何把一个目录及其以下的所有文件在qmake项目中打包
- 探讨C#.NET下DropDownList的一个有趣的bug及其解决办法
- 探讨C#.NET下DropDownList的一个有趣的bug及其解决办法
- android studio在4.4或4.2手机系统出现java.lang.ClassNotFoundException的解决办法
- 在解决方案中的一个或多个项目由于以下原因未能加载的解决方法
- android 2.3 popwindow NullPointerException的解决
- Android - 在一个应用程序中启动另外一个已经安装的应用程序或系统程序
- Android:在一个应用程序中启动另外一个已经安装的应用程序或系统程序
- IP地址排序
- UVA 10130 SuperSale(dp + 01背包)
- 约瑟夫环
- 冒泡排序
- C++ 以函数为单位的程序结构
- PopWindow在Android 2.3.3 或以下的系统的一个bug 及其解决办法
- suse操作系统对应的内核版本列表
- 理解多线程设计模式
- STDIN_FILENO与stdin 的区别
- 选择排序
- experiment : use Aux Library on R0
- MFC消息顺序
- HTML5 缓存: cache manifest
- hadoop 中hdfs启动出错