“Avoid passing null as the view root” warning when inflating view for use by AlertDialog

前端 未结 10 677
面向向阳花
面向向阳花 2020-12-12 15:41

I get the lint warning, Avoid passing null as the view root when inflating views with null as parent, like:

LayoutInfl         


        
相关标签:
10条回答
  • 2020-12-12 16:19

    The short story is that when you are inflating a view for a dialog, parent should be null, since it is not known at View inflation time. In this case, you have three basic solutions to avoid the warning:

    1. Suppress the warning using an @Suppress
    2. Inflate the View using View's inflate method. This is just a wrapper around a LayoutInflater, and mostly just obfuscates the problem.
    3. Inflate the View using LayoutInflater's full method: inflate(int resource, ViewGroup root, boolean attachToRoot). Set attachToRoot to false.This tells the inflater that the parent is not available. In older versions of Android Lint, this removed the warning. This is no longer the case in post 1.0 versions of Android Studio.

    Check out http://www.doubleencore.com/2013/05/layout-inflation-as-intended/ for a great discussion of this issue, specifically the "Every Rule Has an Exception" section at the end.

    0 讨论(0)
  • 2020-12-12 16:22

    You should use AlertDialog.Builder.setView(your_layout_id), so you don't need to inflate it.

    Use AlertDialog.findViewById(your_view_id) after creating the dialog.

    Use (AlertDialog) dialogInterface to get the dialog inside the OnClickListener and then dialog.findViewById(your_view_id).

    0 讨论(0)
  • 2020-12-12 16:26

    According to https://developer.android.com/guide/topics/ui/dialogs

    Inflate and set the layout for the dialog
    Pass null as the parent view because its going in the dialog layout

    therefore, for creating AlertDialog, I use @SuppressLint("InflateParams")

    LayoutInflater inflater = requireActivity().getLayoutInflater();
    @SuppressLint("InflateParams")
    View view = inflater.inflate(R.layout.layout_dialog, null);
    builder.setView(view);
    
    0 讨论(0)
  • 2020-12-12 16:37

    Use this code to inflate the dialog view without a warning:

    View.inflate(context, R.layout.dialog_edit, null);
    
    0 讨论(0)
  • 2020-12-12 16:39

    From the documentation of View.inflate(), it says

    Inflate a view from an XML resource. This convenience method wraps the LayoutInflater class, which provides a full range of options for view inflation.

      @param context The Context object for your activity or application.
      @param resource The resource ID to inflate
      @param root A view group that will be the parent.  Used to properly inflate the  layout_* parameters.
    
    0 讨论(0)
  • 2020-12-12 16:39

    Instead of doing

    view = inflater.inflate(R.layout.list_item, null);
    

    do

    view = inflater.inflate(R.layout.list_item, parent, false);
    

    It will inflate it with the given parent, but won't attach it to the parent.

    Many thanks to Coeffect (link to his post)

    0 讨论(0)
提交回复
热议问题