RelativeLayout measure方法报NullPointerException

问题描述

之前开发中碰到这样一个bug,部分机型会有崩溃现象,错误内容如下:

- E/AndroidRuntime( 3579): FATAL EXCEPTION: main
- E/AndroidRuntime( 3579): java.lang.NullPointerException
- E/AndroidRuntime( 3579):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:431)
- E/AndroidRuntime( 3579):    at android.view.View.measure(View.java:8462)

很奇怪,明明没有什么特别的地方,但是在Android4.3的机器上就是会有崩溃~~~

问题分析

在stackoverflow上发现有人碰到过相同的问题。

  • 问题原因
    RelativeLayout的onMeasure方法中有这样一段代码:
if (isWrapContentWidth) {
            // Width already has left padding in it since it was calculated by looking at
            // the right of each child view
            width += mPaddingRight;

            if (mLayoutParams != null && mLayoutParams.width >= 0) {
                width = Math.max(width, mLayoutParams.width);
            }

            width = Math.max(width, getSuggestedMinimumWidth());
            width = resolveSize(width, widthMeasureSpec);

看起来没有问题,但是对比Android 4.3源码,

if (mLayoutParams.width >= 0) {
      width = Math.max(width, mLayoutParams.width);
}

这也就是为什么会在Android4.3手机上出现崩溃的原因之一。
再深入探究一下mLayoutParams这个参数为什么会是null。首先看代码的使用方法:

contentView = (ViewGroup) LayoutInflater.
               rom(context).inflate(R.layout.select_text_oprate_window, null);

这句代码的意思是,从xml中实例化View视图,但是父视图为空,所以从xml实例化的View视图没有办法attach到View层次树中。那么这个和mLayoutParams有什么关系??~~~
首先看看mLayoutParams这个参数表示什么意思:

/**
     * The layout parameters associated with this view and used by the parent
     * {@link android.view.ViewGroup} to determine how this view should be
     * laid out.
     * {@hide}
     */
    protected ViewGroup.LayoutParams mLayoutParams;

从注释中可以看出,这个参数表示子View布局的期望值,实际值由父View,或者说整个View树决定。

 /**
     * Get the LayoutParams associated with this view. All views should have
     * layout parameters. These supply parameters to the <i>parent</i> of this
     * view specifying how it should be arranged. There are many subclasses of
     * ViewGroup.LayoutParams, and these correspond to the different subclasses
     * of ViewGroup that are responsible for arranging their children.
     *
     * This method may return null if this View is not attached to a parent
     * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
     * was not invoked successfully. When a View is attached to a parent
     * ViewGroup, this method must not return null.
     *
     * @return The LayoutParams associated with this view, or null if no
     *         parameters have been set yet
     */
    @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
    public ViewGroup.LayoutParams getLayoutParams() {
        return mLayoutParams;
    }

在来看这个函数的get方法。注释里面写了两条:1、mLayoutParams这个参数必须设置;2、当View视图attach到父视图(View层次树),mLayoutParams这个参数肯定不会为null。

问题解决

回到项目代码,

contentView = (ViewGroup) LayoutInflater.
             rom(context).inflate(R.layout.select_text_oprate_window, null);

首先,这种inflate方法没有父布局,所以也没有attach这一说~~~
再者,项目代码中没有显示调用contentView.setLayoutParams(params)方法。按照源码中的说法,这个参数可能报NullPointerException。(为什么是可能??!!)
解决方法:

  • 采用一般建议的inflate方法
contentView = (ViewGroup) LayoutInflater.
             rom(context).inflate(R.layout.select_text_oprate_window, parent,false);
  • 显示setLayoutParams
contentView = (ViewGroup) LayoutInflater.
                from(context).inflate(R.layout.select_text_oprate_window, null);

 ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        contentView.setLayoutParams(params);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • RecyclerView Item 布局宽高无效问题探究 前言 这个问题很早之前就碰到过,后来通过google找到...
    TinyMen阅读 3,146评论 0 0
  • 一、适用场景 ListViewListview是一个很重要的组件,它以列表的形式根据数据的长自适应展示具体内容,用...
    Geeks_Liu阅读 13,677评论 1 28
  • 这是FUN无界的第24篇文章 因为下雨,预计的装修只好继续延期。江南的梅雨季节就像小孩脸:忽阴忽晴,和我们捉迷藏;...
    FUN无界阅读 3,089评论 0 1
  • 一个人要活得好,如果从某个角度来说,首先是有一定的物质基础,然后,才是精神生活。然而,这个基础因人而异,而有的人永...
    米漾的爱阅读 1,666评论 0 0
  • 今天花了半小时给李总写感恩节的话语,昨天看到那个小孩 坐下来和她好好招呼逗她,昨天花了很长时间买给宝宝老师的花 花...
    nicolaspanda阅读 1,040评论 0 0

友情链接更多精彩内容