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
widththe popup's widthheightthe popup's height 

这实际上要是指定了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 widthint height) {
250
         this(nullwidthheight);     
251
     } 
        

Step  2:
 
265
     public PopupWindow(View contentViewint widthint height) {
266
         this(contentViewwidthheightfalse);  // ->
contentView = null
267
     }

Step 3:
281
     public PopupWindow(View contentViewint widthint heightboolean focusable) {
282
         if (contentView != null) {
283
             mContext = contentView.getContext();
284
             mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
285
         }
286
         setContentView(contentView);     // -> 在这里报了个NullPointExcetion ,再跟踪
287
         setWidth(width);
288
         setHeight(height);
289
         setFocusable(focusable);
290
     }


 Step 4 :
2.3.3
系统下
376
     public void setContentView(View contentView) {
377
         if (isShowing()) {
378
             return;
379
         }
380
 
381
         mContentView = contentView;
382
 
383
         if (mContext == null) {
384
             mContext = mContentView.getContext();
385
         }
386
 
387
         if (mWindowManager == null) {
388
             mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
389
         }
390
     }
 同是
Step 4 :
4.0.4 系统下
390
     public void setContentView(View contentView) {
391
         if (isShowing()) {
392
             return;
393
         }
394
 
395
         mContentView = contentView;
396
 
397
         if (mContext == null && mContentView != null) {
398
             mContext = mContentView.getContext();
399
         }
400
 
401
         if (mWindowManager == null && mContentView != null) {
402
             mWindowManager = (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 view
3、mPopupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
mPopupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
这里的setWidth() setHeight() 才是是改变PopupWindow 的Width/Height MeasureSpec

转载请说明出处,鼓励原创!
原创粉丝点击