279

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

410

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.

Sign up to request clarification or add additional context in comments.

11 Comments

@Coeffect but what should I use when inflating from within the Activity? What should I use instead of parent?
@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.
The same problem (no parent) comes up when implementing onCreateInputView() for an InputMethodService.
Crap, this is a better answer.. stackoverflow.com/questions/26404951/…
what about custom view for alertdialog, what does parent should be?
|
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

Comments

59

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 Comment

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.
39

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 Comments

It would be better to do a suppression than to trick lint into working.
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.
To suppress Lint for this one, add @SuppressLint("InflateParams") above your method.
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.
12

For AlertDialog bellow code can be used

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

Comments

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.

Comments

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 Comment

by the way ... import ANDROIDX.fragment.app.Fragment;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.