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.

You are watching: Avoid passing null as the view root

public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context _context; private List _listDataHeader; // header titles // child data in format of header title, child title private HashMap> _listDataChild; public ExpandableListAdapter(Context context, List listDataHeader, HashMap> 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 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._conmessage .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 View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) String headerTitle = (String) getGroup(groupPosition); if (convertView == null) LayoutInflater infalInflater = (LayoutInflater) this._conmessage .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;

*

*

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.


*

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://carolannpeacock.com/a/5027921/1065835


*

*

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);
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);
For AlertDialog bellow code can be used

convertView = layoutInflater.inflate(R.layout.list_item, findViewById(android.R.id.content), false);
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.


Here is a picture for reference:

*

return inflater.inflate(R.layout.some_layout, container, true); // for the last parameter use true, false or simply don"t declare
Thanks for contributing an answer to Stack Overflow!

Please be sure to answer the question. Provide details and share your research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.

See more: Why Are People Using Angry Face Reactions On Facebook Angry Face Meme

To learn more, see our tips on writing great answers.


Post Your Answer Dismap

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy


Not the answer you're looking for? Browse other questions tagged android android-layout warnings layout-inflater or ask your own question.


Why does LayoutInflater ignore the layout_width and layout_height layout parameters I've specified?