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

萝らか妹 提交于 2019-12-27 16:25:43

问题


Passing null for root studio gives me this warning:

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

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

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;
    }

}

回答1:


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.




回答2:


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

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

Update #1.

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




回答3:


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 = View.inflate(context,R.layout.custom_layout,null);



回答4:


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:

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



回答5:


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.

The ViewGroup been 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.




回答6:


For AlertDialog bellow code can be used

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



回答7:


here is a picture for reference

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


来源:https://stackoverflow.com/questions/24832497/avoid-passing-null-as-the-view-root-need-to-resolve-layout-parameters-on-the-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!