Android 为 AlertDialog 使用膨胀视图时,“避免将 null 作为视图根传递”警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26404951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Avoid passing null as the view root" warning when inflating view for use by AlertDialog
提问by Randy Sugianto 'Yuku'
I get the lint warning, Avoid passing null as the view root
when inflating views with null
as parent
, like:
Avoid passing null as the view root
当使用null
as膨胀视图时,我收到 lint 警告parent
,例如:
LayoutInflater.from(context).inflate(R.layout.dialog_edit, null);
However, the view is to be used as the content of an AlertDialog
, using setView
on AlertDialog.Builder
, so I don't know what should be passed as the parent
.
但是,视图要用作 an 的内容AlertDialog
,使用setView
on AlertDialog.Builder
,所以我不知道应该将什么作为parent
.
What do you think the parent
should be in this case?
你认为parent
在这种情况下应该是什么?
回答by Edward Brey
Use this code to inflate the dialog view without a warning:
使用此代码在没有警告的情况下扩展对话框视图:
View.inflate(context, R.layout.dialog_edit, null);
回答by emerssso
The short story is that when you are inflating a view for a dialog, parent
should be null, since it is not known at View inflation time. In this case, you have three basic solutions to avoid the warning:
简短的故事是,当您为对话框膨胀视图时,parent
应该为空,因为它在视图膨胀时间是未知的。在这种情况下,您可以使用三种基本解决方案来避免警告:
- Suppress the warning using an @Suppress
- Inflate the View using View's inflate method. This is just a wrapper around a LayoutInflater, and mostly just obfuscates the problem.
Inflate the View using LayoutInflater's full method:In older versions of Android Lint, this removed the warning. This is no longer the case in post 1.0 versions of Android Studio.inflate(int resource, ViewGroup root, boolean attachToRoot)
. SetattachToRoot
tofalse
.This tells the inflater that the parent is not available.
- 使用 @Suppress 抑制警告
- 使用视图的膨胀方法膨胀视图。这只是一个 LayoutInflater 的包装器,主要是混淆了问题。
使用充气LayoutInflater的查看完整的方法:在旧版本的 Android Lint 中,这消除了警告。在 1.0 版的 Android Studio 中不再是这种情况。inflate(int resource, ViewGroup root, boolean attachToRoot)
。设置attachToRoot
为false
。这告诉充气机父级不可用。
Check out http://www.doubleencore.com/2013/05/layout-inflation-as-intended/for a great discussion of this issue, specifically the "Every Rule Has an Exception" section at the end.
查看http://www.doubleencore.com/2013/05/layout-inflation-as-intended/对这个问题的一个很好的讨论,特别是最后的“每个规则都有一个例外”部分。
回答by SVL Narasimham
Casting null as ViewGroup resolved the warning:
将 null 转换为 ViewGroup 解决了警告:
View dialogView = li.inflate(R.layout.input_layout,(ViewGroup)null);
where li
is the LayoutInflater's
object.
其中li
是LayoutInflater's
对象。
回答by Jeffrey Chen
You should use AlertDialog.Builder.setView(your_layout_id)
, so you don't need to inflate it.
你应该使用AlertDialog.Builder.setView(your_layout_id)
,所以你不需要膨胀它。
Use AlertDialog.findViewById(your_view_id)
after creating the dialog.
AlertDialog.findViewById(your_view_id)
创建对话框后使用。
Use (AlertDialog) dialogInterface
to get the dialog
inside the OnClickListener
and then dialog.findViewById(your_view_id)
.
使用(AlertDialog) dialogInterface
来获取dialog
里面的OnClickListener
,然后dialog.findViewById(your_view_id)
。
回答by kjdion84
You don't need to specify a parent
for a dialog.
您不需要parent
为对话框指定 a 。
Suppress this using @SuppressLint("InflateParams")
at the top of the override.
使用@SuppressLint("InflateParams")
覆盖的顶部来抑制它。
回答by Mousa
When you really don't have any parent
(for example creating view for AlertDialog
), you have no other choice than passing null
. So do this to avoid warning:
当你真的没有任何东西时parent
(例如创建视图AlertDialog
),你别无选择,只能通过null
。所以这样做以避免警告:
final ViewGroup nullParent = null;
convertView = infalInflater.inflate(R.layout.list_item, nullParent);
回答by Apfelsaft
The AlertDialog is as far as I know the only case were you can safely use nullinstead of a parent view. In this case you can suppress the warning by using:
@SuppressLint("InflateParams")
In general you should never use SupressLint or one of the workarounds mentioned in the other answers to get rid of the warning. The parent view is necessary to evaluate the Layout Params which are declared in the root element of the View being inflated. That means if you use nullinstead of the parent view, all Layout Params in the root element will be ignored and replaced by default Layout Params. Most of the time it will be fine, but in some cases it will result in a really hard-to-find bug.
据我所知, AlertDialog 是您可以安全地使用null而不是父视图的唯一情况。在这种情况下,您可以使用以下方法抑制警告:
@SuppressLint("InflateParams")
一般来说,您永远不应该使用 SupressLint 或其他答案中提到的解决方法之一来消除警告。父视图对于评估在被膨胀的视图的根元素中声明的布局参数是必要的。这意味着如果您使用null而不是父视图,则根元素中的所有布局参数都将被忽略并替换为默认布局参数。大多数时候它会很好,但在某些情况下它会导致一个很难找到的错误。
回答by Divya Gupta
From the documentation of View.inflate()
, it says
从 的文档中View.inflate()
,它说
Inflate a view from an XML resource. This convenience method wraps the
LayoutInflater
class, which provides a full range of options for view inflation.
从 XML 资源扩充视图。这种方便的方法包装了
LayoutInflater
类,它为视图膨胀提供了全方位的选项。
@param context The Context object for your activity or application.
@param resource The resource ID to inflate
@param root A view group that will be the parent. Used to properly inflate the layout_* parameters.
回答by Cor
According to https://developer.android.com/guide/topics/ui/dialogs
根据https://developer.android.com/guide/topics/ui/dialogs
Inflate and set the layout for the dialog
Pass null as the parent view because its going in the dialog layout
膨胀并设置对话框的布局 将
null 作为父视图传递,因为它进入对话框布局
therefore, for creating AlertDialog, I use @SuppressLint("InflateParams")
因此,为了创建 AlertDialog,我使用 @SuppressLint("InflateParams")
LayoutInflater inflater = requireActivity().getLayoutInflater();
@SuppressLint("InflateParams")
View view = inflater.inflate(R.layout.layout_dialog, null);
builder.setView(view);
回答by Christian
Instead of doing
而不是做
view = inflater.inflate(R.layout.list_item, null);
do
做
view = inflater.inflate(R.layout.list_item, parent, false);
It will inflate it with the given parent, but won't attach it to the parent.
它将使用给定的父级对其进行充气,但不会将其附加到父级。
Many thanks to Coeffect (link to his post)
非常感谢 Coeffect(链接到他的帖子)