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

前端 未结 7 662
旧时难觅i
旧时难觅i 2020-11-27 10:13

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 r

相关标签:
7条回答
  • 2020-11-27 10:15

    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 讨论(0)
  • 2020-11-27 10:20

    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);
    
    0 讨论(0)
  • 2020-11-27 10:20

    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
    
    0 讨论(0)
  • 2020-11-27 10:33

    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);
    
    0 讨论(0)
  • 2020-11-27 10:34

    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.

    0 讨论(0)
  • 2020-11-27 10:40

    For AlertDialog bellow code can be used

    convertView = layoutInflater.inflate(R.layout.list_item, findViewById(android.R.id.content), false);
    
    0 讨论(0)
提交回复
热议问题