276

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

}

7 Answers 7

409

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.

11
  • 40
    @Coeffect but what should I use when inflating from within the Activity? What should I use instead of parent? Oct 16, 2014 at 18:40
  • 3
    @AlexanderKuznetsov Depends what you're trying to do, I suppose. If you're trying to set the content for the activity, you should be using setContentView(layoutId). If you're trying to add a new view to an existing ViewGroup, you should probably just pass the parent and let the inflater attach the new view.
    – Coeffect
    Oct 21, 2014 at 17:20
  • 5
    The same problem (no parent) comes up when implementing onCreateInputView() for an InputMethodService.
    – Ted Hopp
    Jan 1, 2015 at 17:11
  • 1
    Crap, this is a better answer.. stackoverflow.com/questions/26404951/…
    – Siddharth
    Aug 23, 2015 at 16:17
  • 6
    what about custom view for alertdialog, what does parent should be?
    – user25
    Jul 6, 2018 at 18:04
62

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

0
57

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);
1
  • 6
    This gets rid of the Lint warning but not the problem itself as View.inflate() calls LayoutInflater under the hood. You should fix the cause, not just remove the message.
    – lsrom
    Feb 16, 2021 at 10:20
37

Edit:

At the time of writing this answer, I was not aware of Lint suppressions. It's better to suppress Lint instead of tricking the IDE. Add this above the line or method:

@SuppressLint("InflateParams")

Old answer:

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);
4
  • 75
    It would be better to do a suppression than to trick lint into working.
    – StarWind0
    Sep 12, 2016 at 23:07
  • 4
    I'm not sure this answer is correct. I am successfully using ViewGroup root = (ViewGroup) myActivity.findViewById(R.id.my_main_content); where my_main_content is the id of the outermost container in my activity's layout file. Nov 30, 2016 at 23:10
  • 13
    To suppress Lint for this one, add @SuppressLint("InflateParams") above your method. Oct 5, 2017 at 19:13
  • I found @ban-geoengineering reply to be the most helpful it both got rid of the message and fixed the issue. Here is the Kotlin solution that worked foe me. val rootView = findViewById<ViewGroup>(R.id.container) .... imageHolder = vInflater.inflate(R.layout.recycler_view_target, rootView,false) This allowed me to place different Views within a RecyclerView Holder.
    – BroPage
    Aug 16, 2021 at 14:59
12

For AlertDialog bellow code can be used

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

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.

2

Here is a picture for reference:

enter image description here

return inflater.inflate(R.layout.some_layout, container, true); 
// for the last parameter use true, false or simply don't declare
1
  • by the way ... import ANDROIDX.fragment.app.Fragment; Nov 10, 2019 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.