本文主要是介绍PopWindow在Android 2.3.3 或以下的系统的一个bug 及其解决办法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
public PopupWindow (int width, int height)
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
width | the popup's width |
---|---|
height | the popup's height |
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}
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 及其解决办法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!