275

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

}
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
hash
  • 5,336
  • 7
  • 36
  • 59

7 Answers7

407

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.

Coeffect
  • 8,772
  • 2
  • 27
  • 42
  • 39
    @Coeffect but what should I use when inflating from within the Activity? What should I use instead of parent? – Alexander Kuznetsov Oct 16 '14 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 '14 at 17:20
  • @LucasTan I guess I'd keep a reference to the TabHost in the TabContentFactory and use that as the parent when inflating, but I'm not sure if that's the best solution. It is strange that the createTabContent method doesn't supply parent/context. – Coeffect Nov 10 '14 at 02:18
  • 5
    The same problem (no parent) comes up when implementing `onCreateInputView()` for an `InputMethodService`. – Ted Hopp Jan 01 '15 at 17:11
  • Also if you are just inflating a view to customize it(e.g. if you want to customize a image view at run time), you can't send any view as parent there – Shirish Herwade Feb 27 '15 at 10:02
  • 1
    Crap, this is a better answer.. http://stackoverflow.com/questions/26404951/avoid-passing-null-as-the-view-root-warning-when-inflating-view-for-use-by-ale – Siddharth Aug 23 '15 at 16:17
  • Can someone please give a hint about "What is the meaning of _attach to parent_"? – Bugs Happen Aug 23 '15 at 17:59
  • 6
    what about custom view for alertdialog, what does parent should be? – user25 Jul 06 '18 at 18:04
  • @AlexanderKuznetsov view = View.inflate(context,R.layout.custom_layout,null); – Kishan Solanki Jan 11 '21 at 08:22
  • I can't find where the parent is – Hanako Jan 18 '21 at 18:08
  • This does not work and generates a crash! – Herman Van Der Blom Mar 24 '22 at 12:12
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

Rautermann
  • 334
  • 3
  • 10
Maksim Dmitriev
  • 5,985
  • 12
  • 73
  • 138
56

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);
Thunderstick
  • 1,193
  • 13
  • 12
  • 4
    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 '21 at 10:20
36

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);
Mousa
  • 2,190
  • 3
  • 21
  • 34
  • 74
    It would be better to do a suppression than to trick lint into working. – StarWind0 Sep 12 '16 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. – ban-geoengineering Nov 30 '16 at 23:10
  • 13
    To suppress Lint for this one, add `@SuppressLint("InflateParams")` above your method. – ban-geoengineering Oct 05 '17 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(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 '21 at 14:59
12

For AlertDialog bellow code can be used

convertView = layoutInflater.inflate(R.layout.list_item, findViewById(android.R.id.content), false);
Mostafa Monowar
  • 881
  • 1
  • 8
  • 14
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.

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
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
Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69