• android窗口泄漏,isInEditMode解决可视化编辑器无法识别自定义控件的问题


    android窗口泄漏

    在做项目是遇到这个错误:google:WindowManager: Activity has leaked window.

    产 生原因:我们知道Android的每一个Activity都有个WindowManager窗体管理器,同样构建在某个Activity之上的对话框、 PopupWindow也有相应的WindowManager窗体管理器。因为对话框、PopupWindow不能脱离Activity而单独存在着,所 以当某个Dialog或者某个PopupWindow正在显示的时候我们去finish()了承载该Dialog或PopupWindow的 Activity时,就会抛WindowLeaked异常了,因为这个Dialog或PopupWindow的WindowManager已经没有谁可以 附属了,所以它的窗体管理器已经泄漏了。

    Activity中create一个Dialog,若你先关闭Dialog再关闭Activity就是正常的,若你先关闭Activity再关闭Dialog就会报错这个android.view.WindowLeaked错误了。

    分析这个原因是:Dialog是基于Activity而创建的new Dialog(this);this就是Activity。 Activtity先finish,那Dialog就没得依附了,所以就会报android.view.WindowLeaked。

    还有如果是WindowManager通过addView方法加上去的view,在activity退出之前一定要调用removeView,否则也会产生窗口泄漏。

    使用isInEditMode解决可视化编辑器无法识别自定义控件的问题

    isInEditMode:

    Indicates whether this View is currentlyin edit mode. A View is usually in edit mode when displayed within a developertool. For instance, if this View is being drawn by a visual user interfacebuilder, this method should return true. Subclasses should check the returnvalue of this method to provide different behaviors if their normal behaviormight interfere with the host environment. For instance: the class spawns athread in its constructor, the drawing code relies on device-specific features,etc. This method is usually checked in the drawing code of custom widgets.

    如果在自定义控件的构造函数或者其他绘制相关地方使用系统依赖的代码,会导致可视化编辑器无法报错并提示:Use View.isInEditMode() in your custom views to skip code when shownin Eclipse

    比如:

    public class LockRelativeLayout extends RelativeLayout {    private Handler mainHandler = null; //与主Activity通信的Handler对象    public LockRelativeLayout(Context context, AttributeSet attrs) {        super(context, attrs, 0);        mContext = context;        if (isInEditMode()) { return; }        mainHandler = ((SPActivity)mContext).getMHandler();    }}

    如果不加上if(isInEditMode()) { return; },标红处代码会导致可视化编辑报错。

  • 相关阅读:
    【.net】未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序解决办法
    【JS】JavaScript中Null和undefind区别
    【SQL】SQL中on条件与where条件的区别
    【C#】C#创建自定义Object对象
    【.NET】asp.net Redirect 图片路径
    【JQ】jq动态绑定事件.on()、解绑事件off()
    【.NET】using 语句中使用的类型必须可隐式转换为"System.IDisposable"
    C# enum、int、string三种类型互相转换
    js中Date与timestamp(时间戳)的相互转换
    2. 自动化运维系列之Cobbler给Openstack节点安装操作系统。
  • 原文地址:https://www.cnblogs.com/ldq2016/p/5407935.html
Copyright © 2020-2023  润新知