LayoutInflater的错误用法(Avoid passing null as the view root )

2023-05-16

今天在练习使用Fragment的时候,注意到在使用LayoutInflater的时候有黄色报警(Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's root element)),然后搜索了一下,发现国内深入讨论的不多。然后找到了一篇国外对LayoutInflater用法深入研究的文章,根据该文章解决了问题,特此转过来学习。


Layout inflation is the term used within the context of Android to indicate when an XML layout resource is parsed and converted into a hierarchy of View objects.


It’s common practice in the Android SDK, but you may be surprised to find that there is a wrong way to use LayoutInflater, and your application might be one of the offenders. If you’ve ever written something like the following code using LayoutInflater in your Android application:

<span style="font-size:14px;">inflater.inflate(R.layout.my_layout, null);</span>

PLEASE read on, because you’re doing it wrong and I want to explain to you why.

Get to Know LayoutInflater

Let’s first take a look at how LayoutInflater works. There are two usable versions of the inflate() method for a standard application:

<span style="font-size:14px;">inflate(int resource, ViewGroup root)</span>

<span style="font-size:14px;">inflate(int resource, ViewGroup root, boolean attachToRoot)</span>

The first parameter points to the layout resource you want to inflate. The second parameter is the root view of the hierarchy you are inflating the resource to attach to. When the third parameter is present, it governs whether or not the inflated view is attached to the supplied root after inflation.

It is these last two parameters that can cause a bit of confusion. With the two parameter version of this method, LayoutInflater will automatically attempt to attach the inflated view to the supplied root. However, the framework has a check in place that if you pass null for the root it bypasses this attempt to avoid an application crash.

Many developers take this behavior to mean that the proper way to disable attachment on inflation is by passing null as root; in many cases not even realizing that the three parameter version of inflate() exists. By doing things this way, we also disable another very important function the root view has…but I’m getting ahead of myself.

Examples from the Framework


Let’s examine some situations in Android where the framework expects you as a developer to interactively inflate portions of the view.

Adapters
are the most common case for using LayoutInflater is custom ListView adapters overriding getView(), which has the following method signature:

getView(int position, View convertView, ViewGroup parent)


Fragments also use inflation often when creating views via onCreateView(); notice its method signature:

onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

Have you noticed that every time the framework wants you to inflate a layout, they also pass you the parent ViewGroup it will eventually be attached to? Notice also that in most cases (including the above two examples), it will throw an Exception later on if LayoutInflater is allowed to automatically attach the inflated view to the root.

So why do you suppose we are given this ViewGroup if we are not supposed to attach to it? It turns out the parent view is a very important part of the inflation process because it is necessary in order to evaluate the LayoutParams declared in the root element of the XML being inflated. Passing nothing here is akin to telling the framework “I don’t know what parent this view will be attached to, sorry.”

The problem with this is android:layout_xxx attributes are always be evaluated in the context of the parent view. As a result, without any known parent, all LayoutParams you declared on the root element of your XML tree will just get thrown away, and then you’ll be left asking “why is the framework ignoring the layout customizations I defined? I’d better check SO and then file a bug.”

Without LayoutParams, the ViewGroup that eventually hosts the inflated layout is left to generate a default set for you. If you are lucky (and in many cases you are) these default parameters are the same as what you had in XML…masking the fact that something is amiss.


Application Example

So you claim you’ve never seen this happen in an application? Take the following simple layout that we want to inflate for a ListView row:


R.layout.item_row


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="15dp"
        android:text="Text1" />
    <TextView
        android:id="@+id/text2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Text2" />
</LinearLayout>


We want to set the height of our row to be a fixed height, in this case the preferred item height for the current theme…seems reasonable.

However, when we inflate this layout the wrong way

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = inflate(R.layout.item_row, null);
    }
 
    return convertView;
}

we end up with a result that looks like this



What happened to the fixed height we set?? This is usually where you end up setting the fixed height on all your child views, switching the root elements height to wrap_content, and move on without really knowing why it broke (you may have even cursed at Google in the process).


If we instead inflate the same layout this way

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = inflate(R.layout.item_row, parent, false);
    }
 
    return convertView;
}

we end up with what we expected in the first place.



Hooray!

Every Rule Has An Exception


There are of course instances where you can truly justify a null parent during inflation, but they are few. One such instance occurs when you are inflating a custom layout to be attached to an AlertDialog. Consider the following example where we want to use our same XML layout but set it as the dialog view:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
View content = LayoutInflater.from(context).inflate(R.layout.item_row, null);
 
builder.setTitle("My Dialog");
builder.setView(content);
builder.setPositiveButton("OK", null);
builder.show();

The issue here is that AlertDialog.Builder supports a custom view, but does not provide an implementation of setView() that takes a layout resource; so you must inflate the XML manually. However, because the result will go into the dialog, which does not expose its root view (in fact, it doesn’t exist yet), we do not have access to the eventual parent of the layout, so we cannot use it for inflation. It turns out, this is irrelevant, because AlertDialog will erase any LayoutParams on the layout anyway and replace them with match_parent.

So the next time your fingers are tempted to just type null into inflate(), you should stop and ask yourself “do I really not know where this view will end up?”

Bottom line, you should think of the two parameter version of inflate() as a convenience shortcut to omit true as the third paramter. You should not think of passing null as a convenience shortcut to omit false.

For future insights, please subscribe to our email list at the bottom of the page (below the comments)!


转自:https://possiblemobile.com/2013/05/layout-inflation-as-intended/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

LayoutInflater的错误用法(Avoid passing null as the view root ) 的相关文章

  • 为什么SQL不支持“= null”而不支持“is null”?

    我不是在问if确实如此 我知道事实并非如此 我很好奇原因 我已阅读过诸如此之类的支持文档关于在 MySQL 中使用 Null 值 http dev mysql com doc refman 5 0 en working with null
  • JavaScript 未定义替换为 null

    在 JavaScript 中undefined可以重新分配 因此通常建议创建一个自执行函数 以确保 undefined 实际上是未定义的 作为备选null and undefined肯定是 但任何其他值都大致相当于null undefine
  • 当方法尝试使用可以为空的字段时抛出哪个异常? [复制]

    这个问题在这里已经有答案了 我实际上正在从事框架开发 这意味着需要非常强大的编码方法 我面临一个问题 我不知道需要抛出哪个 System Exception 派生类 基本上情况是 我有一个类 其中的字段可以由构造函数选择性地初始化 并且具有
  • 如何处理 nil 值变量

    我有如下模型 struc Info Decodable var firstName String var lastName String 在表格视图单元格中显示时 我正在做的事情如下 personName text personArray
  • 在 IL 中对空引用调用实例方法

    在 IL 中可以对空引用调用实例方法是否正确 有没有例子可以证明这一点 是的 这是可能的 只要该方法不使用this因为 CLR 不会对以下内容进行空检查call指示 You would have to modify the IL by ha
  • 为什么 Objective-C dispatch_group_create() 可以返回 NULL 而 Swift DispatchGroup() 不能?

    我注意到用于调度组的 Swift API 和 Objective C API 之间存在差异 The init for DispatchGroup https developer apple com documentation dispatc
  • 如何在Rcpp代码中返回R的NULL?

    假设我有一个 C 代码要使用 Rcpp 编译 并将在 R 中调用 Rcpp export SEXP to env List x if x hasAttribute names return x else return NULL 应该做什么N
  • 如何检查 params[:some][:field] 是否为零?

    我尝试了代码 这让很多人受益匪浅 如何测试rails中是否存在参数 https stackoverflow com questions 5629402 how to test if parameters exist in rails 但它不
  • 从 pandas 数据框中删除具有空值的行

    我正在尝试从数据框中删除一行 其中其中一列的值为空 我能找到的大部分帮助都与删除 NaN 值有关 到目前为止 这对我不起作用 我在这里创建了数据框 successfully crated data frame df1 ut get data
  • 如何检查 VBA DAO 记录集中是否为空?

    我在数据库中有一个可选字段 我正在使用 DAO 记录集提取该字段 在将字段与其他字段连接之前 我需要检查该字段是否已设置 到目前为止 我已经尝试过以下代码片段Is and 这是明显错误的语法 Is 无济于事 看来如果我使用 它不会正确地与N
  • Android 自定义组件中的布局未膨胀

    我在自定义视图中遇到空指针异常 源自LinearLayout 因为它找不到它的子视图 这是代码 public class MyView extends LinearLayout public MyView Context context A
  • 如何避免在 Java 中检查空值?

    I use x null避免NullPointerException https docs oracle com javase 9 docs api java lang NullPointerException html 还有其他选择吗 i
  • 如何消除SQL中的NULL字段

    我正在为 SQL Server 2008 R2 开发 TSQL 查询 我正在尝试开发此查询来识别一条记录 客户 由于其中一些值为 NULL 因此我目前正在对大多数表执行 LEFT JOINS 但 LEFT JOIN 的问题是 现在我为某些客
  • 我想向我的销售点的用户授予特权,但我不知道如何做,有什么建议吗?

    我有一个问题 我有一个用netBeans制作的销售点系统 2个用户可以在我的系统中注册 管理员 和 供应商 系统中有几个模块 包括 Inventario 模块允许您在数据库中输入 修改 删除和搜索文章 问题是我希望只有管理员有权限进入该模块
  • 在C语言中,NULL指针和指向0的指针有区别吗?如果是这样,那又怎样?

    在C语言中 NULL指针和指向0的指针有什么区别 ISO IEC 9899 TC2 中规定6 3 2 3 Pointers 3 值为 0 的整型常量表达式 或这样的表达式 强制转换为 void 类型 称为空指针常量 55 如果 null 指
  • Shell Linux:grep 带有 NULL 字符的精确句子

    我有一个像这样的文件 key 0value n akey 0value n key2 0value n 我必须创建一个以单词作为参数的脚本 我必须返回具有与参数完全相同的键的每一行 I tried grep aF key x0 但 grep
  • 带汇总总计和小计

    我有一个脚本可以生成几乎已经存在的结果集 我正在尝试获取小计和总计 我在年份栏中得到了小计 在最后得到了总计 我的目标是让最终结果显示 总计 而不是小计 请注意 由于汇总函数 我的最后一行 位置 也返回为空 SELECT YEAR COUN
  • 如何从 tcl 列表中删除空元素

    你好 我有以下清单 设置 qprList 12345 12345
  • 在 C# 中追加到空字符串是如何工作的?

    我很惊讶地看到一个字符串被初始化为 null 然后在生产环境中附加一些内容的示例 只是闻起来不对劲 我确信它会抛出空对象异常 但这个大大简化的示例也有效 string sample null sample test sample equal
  • 非 Activity 类中的 findViewById

    对此我仍然相对较新 在我在活动类 MainActivity 中使用的非活动类 MyLocation 中查找视图时遇到问题 我正在使用 MyLocation 来获取经度和纬度 我想在使用 GPS 或网络时突出显示文本视图 为此 我需要在非活动

随机推荐

  • C++使用libcurl做HttpClient

    C 43 43 使用libcurl做HttpClient 分类 xff1a 基础技术分享 2012 06 14 19 25 1469人阅读 评论 3 收藏 举报 当使用C 43 43 做HTTP客户端时 xff0c 目前通用的做法就是使用l
  • CAN的报文格式

    CAN的报文格式 在总线中传送的报文 xff0c 每帧由7部分组成 CAN协议支持两种报文格式 xff0c 其唯一的不同是标识符 xff08 ID xff09 长度不同 xff0c 标准格式为11位 xff0c 扩展格式为29位 在标准格式
  • uabntu系统安装软件:E: 无法定位软件包问题

    血泪教训 耽误了贼长时间 一开始参考了如下链接 xff0c 然而我的问题一个没解决 xff0c 哭了 Ubuntu sudo apt get install 出现 E 无法定位软件包问题 解决方法汇总 遥想在想peach的博客 CSDN博客
  • 右手定则

    http www 7wenta com zhuanti 9648 html 问他 首页问答中心竞技场 学习快报 达人榜商城 问他手机版 注册 登录 问他网 gt 学习专题 gt 高中物理 gt 右手定则 物理题库练习题我要提问
  • 【机器人规划】Bug解析

    文章目录 参考文献简介Bug1算法Bug2算法Tangent Bug算法关于O i的选择激光雷达半径对算法的影响 总结 参考文献 自动驾驶决策规划算法 Bug Algorithms Bug算法 Robotic Motion Planning
  • ubantu虚拟机无法联网

    在VMware中安装Ubuntu虚拟机 xff0c 总会发生无法上网的情况 xff0c 主要情况有以下几点 xff1a 宿主机可以上网 xff1b 虚拟机却无法访问网页虚拟机ping不通任何网站 xff0c 用浏览器显示error 一般情况
  • 半监督语义分割论文学习记录

    Semi Supervised Semantic Segmentation with Cross Consistency Training 1 1 motivation 一致性训练的目的是在应用于输入的小扰动上增强模型预测的不变性 因此 x
  • 使用Qt二次开发周立功CAN(一)

    使用Qt二次开发周立功CAN xff08 一 xff09 使用Qt二次开发周立功的CAN通信 xff0c 第一步需要完成动态链接库的加载 xff0c 成功加载之后才能调用其提供的接口函数 加载库需要注意的问题有两个 xff1a 一是Qt版本
  • 字节序基础知识

    在各种计算机体系结构中 xff0c 对于字节 字等的存储机制有所不同 xff0c 因而引发了计算机通信领 域中一个很重要的问题 xff0c 即通信双方交流的信息单元 xff08 比特 字节 字 双字等等 xff09 应该以什么样的顺序进行传
  • vlc命令行: 转码 流化 推流

    写在命令行之前的话 xff1a VLC不仅仅可以通过界面进行播放 xff0c 转码 xff0c 流化 xff0c 也可以通过命令行进行播放 xff0c 转码和流化 还可以利用里面的SDK进行二次开发 vlc命令行使用方法 xff1a 1 x
  • C++ 简单实现HTTP GET/POST 请求

    HTTP 超文本传输协议 是一种客户端与服务端的传输协议 xff0c 最早用于浏览器和服务器之间的通信 xff0c 后来因为其使用灵活 方便等特点 xff0c 广泛用于客户端与服务端的通信 文章将简单介绍HTTP协议 xff0c 同时以C
  • STM32单片机HAL库下串口接收不定长数据

    xff33 xff34 xff2d xff13 xff12 单片机 xff28 xff21 xff2c 库下串口接收不定长数据 xff28 xff21 xff2c 库下的串口接收不定长数据代码配置代码实现代码演示总结 xff28 xff21
  • C++将一个数据格式化为固定长度的字符串

    经常会遇到将数据解析为文本文件的现象 xff0c 通常因为数据长度的不同导致 xff0c 可视化效果不好 写一个输入数据获取固定长度字符串的函数 xff0c 来得到一个固定长度的数据 xff0c 让格式化看起来好看一些 include lt
  • Socket原理与编程基础

    一 Socket简介 Socket是进程通讯的一种方式 xff0c 即调用这个网络库的一些API函数实现分布在不同主机的相关进程之间的数据交换 几个定义 xff1a xff08 1 xff09 IP地址 xff1a 即依照TCP IP协议分
  • mac 安装brew

    起因是这样的 xff0c 我想在mac上安装htop 然后我了解到可以用brew安装htop 然后再执行命令 brew install htop 所以我就开始吭哧吭哧安装brew 过程xue wei 曲折了一些 先是看到一个文章 xff0c
  • 【项目学习】C++实现高并发服务器——代码学习(三)用户注册登录功能

    项目来源 xff1a WebServer 上一篇 xff1a 存储解析HTTP请求报文 xff0c 创建响应报文 本文介绍以下功能的代码实现 利用RAII机制实现了数据库连接池 xff0c 减少数据库连接建立与关闭的开销 xff0c 同时实
  • 用CSS3实现动画进度条

    CSS3的新特性为我们实现漂亮的进度条扫清了障碍 xff0c 我们可以完全不需要任何图片和简单的Javascript代码就可以构建 一 第一个例子 效果图 xff1a Demo地址 xff1a http namepk sinaapp com
  • tcpdump命令使用详解

    tcpdump命令使用详解 疯狂的小企鹅的博客 CSDN博客 tcpdump命令详解全网最详细的 tcpdump 使用指南 王一白 博客园Tcpdump抓包工具实战教程 xff0c 让你知道一个抓包走天下 xff01 哔哩哔哩 bilibi
  • Chrome浏览器Postman插件安装包及安装教程

    最近电脑装了新环境 xff0c 以前本地的postman安装包竟然找不到了 xff0c 网上费尽心力找了很多资源 xff0c 终于找到纯净的安装包 xff0c 绕开套路 xff0c 现将Postman安装包及安装教程分享给各位 xff0c
  • LayoutInflater的错误用法(Avoid passing null as the view root )

    今天在练习使用Fragment的时候 xff0c 注意到在使用LayoutInflater的时候有黄色报警 xff08 Avoid passing null as the view root needed to resolve layout