zoukankan      html  css  js  c++  java
  • 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; },标红处代码会导致可视化编辑报错。

  • 相关阅读:
    tracteroute路由追踪
    搭建Weblogic服务器
    Logview_pro破解版
    Spring Boot 如何在类中应用配置文件
    使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)
    springboot 项目中控制台打印日志以及每天生成日志文件
    springboot输出日志到指定目录,简单粗暴,springboot输出mybatis日志
    spring boot 发布成包所需插件
    spring注解
    Multicast注册中心
  • 原文地址:https://www.cnblogs.com/ldq2016/p/5407935.html
Copyright © 2011-2022 走看看