Android 避免将 null 作为视图根传递(需要在膨胀布局的根元素上解析布局参数)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24832497/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:50:04  来源:igfitidea点击:

Avoid passing null as the view root (need to resolve layout parameters on the inflated layout's root element)

androidandroid-layoutwarningslayout-inflater

提问by hash

Passing null for root studio gives me this warning:

为 root studio 传递 null 给了我这个警告:

Avoid passing null as the view root (need to resolve layout parameters on the inflated layout's root element)

避免将 null 作为视图根传递(需要在膨胀布局的根元素上解析布局参数)

It is showing a null value in getGroupView. Please help.

它在 中显示空值getGroupView。请帮忙。

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> listChildData) {
        super();
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);


        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}

回答by Coeffect

Instead of doing

而不是做

convertView = infalInflater.inflate(R.layout.list_item, null);

do

convertView = infalInflater.inflate(R.layout.list_item, parent, false);

It will inflate it with the given parent, but won't attach it to the parent.

它将使用给定的父级对其进行充气,但不会将其附加到父级。

回答by Maksim Dmitriev

I found a good article about LayoutInflater. The author explains how all the versions of the inflatemethod work and gives examples of ListViewand AlertDialog

我找到了一篇关于LayoutInflater. 作者解释了该inflate方法的所有版本是如何工作的,并给出了示例ListViewAlertDialog

http://www.doubleencore.com/2013/05/layout-inflation-as-intended/

http://www.doubleencore.com/2013/05/layout-inflation-as-intended/

Update #1.

更新#1。

This answer recently helped me, too. https://stackoverflow.com/a/5027921/1065835

这个答案最近也帮助了我。 https://stackoverflow.com/a/5027921/1065835

回答by Thunderstick

Here ya go, for some reason using View.inflate instead of inflating from a layoutinflater makes the lint error disappear. Thought I'd post this here since this thread is at the top of the Google Search...

好了,出于某种原因,使用 View.inflate 而不是从 layoutinflater 充气会使 lint 错误消失。我想我会在这里发布这个,因为这个线程在谷歌搜索的顶部......

view = View.inflate(context,R.layout.custom_layout,null);

回答by Mousa

When you really don't have any parent(for example creating view for AlertDialog), you have no other way than passing null. So do this to avoid warning:

当你真的没有任何东西时parent(例如创建视图AlertDialog),你别无他法,只能通过null。所以这样做以避免警告:

final ViewGroup nullParent = null;
convertView = layoutInflater.inflate(R.layout.list_item, nullParent);

回答by Mostafa Monowar

For AlertDialogbellow code can be used

对于AlertDialog波纹管代码可以使用

convertView = layoutInflater.inflate(R.layout.list_item, findViewById(android.R.id.content), false);

回答by Ojonugwa Jude Ochalifu

A nice information I found while looking for a way to solve this.According to Dave Smith,

我在寻找解决此问题的方法时发现了一个很好的信息。根据戴夫史密斯的说法,

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.

布局膨胀是在 Android 上下文中使用的术语,用于指示何时解析 XML 布局资源并将其转换为 View 对象的层次结构。

The ViewGroupbeen asked for here as part of the inflate method parameters is used to inherit higher level styling.While passing null can seem harmless, it can actually cause serious problems for your app later on. Read more about this here.

这里ViewGroup被要求作为 inflate 方法的一部分参数用于继承更高级别的样式。虽然传递 null 看起来无害,但它实际上可能会在以后为您的应用程序带来严重问题。在此处阅读更多相关信息 。

回答by Энтони Чехов

here is a picture for reference

这是一张图片供参考

    return inflater.inflate(R.layout.some_layout, container, true); 
//or false or even not declare