Home » Android » How to fix "Avoid passing null as the view root" in android?

How to fix "Avoid passing null as the view root" in android?

Posted by: admin June 15, 2020 Leave a comment

Questions:

In my android app, I create a dialog like this:

private void handleEdit() {
    LayoutInflater inflater = getLayoutInflater();
    View dialoglayout = inflater.inflate(R.layout.dialog_gallery, null);

    final AlertDialog d = new AlertDialog.Builder(this)
    .setView(dialoglayout)
    .setTitle(R.string.edit)
    .setNegativeButton(R.string.cancel, null)
    .create();

    CheckBox mainCB = (CheckBox)dialoglayout.findViewById(R.id.main);
    CheckBox usedCB = (CheckBox)dialoglayout.findViewById(R.id.used);
    mainCB.setChecked(image.getIsMain());
    usedCB.setChecked(image.getApproved());

    mainCB.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            if (Network.isNetworkAvailable(GalleryScreen.this)) {
                new Async_update_image_state(GalleryScreen.this, fish, image, !image.getIsMain(), image.getApproved(), false);
                d.dismiss();
            } else {
                Popup.ShowErrorMessage(GalleryScreen.this, R.string.no_internet, false);
            }
        }
    });

    usedCB.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            if (Network.isNetworkAvailable(GalleryScreen.this)) {
                new Async_update_image_state(GalleryScreen.this, fish, image, false, !image.getApproved(), true);
                d.dismiss();
            } else {
                Popup.ShowErrorMessage(GalleryScreen.this, R.string.no_internet, false);
            }
        }
    });

    d.show();
}

But I get a warning on View dialoglayout = inflater.inflate(R.layout.dialog_gallery, null); underlining the null.

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

What does this mean and how can I fix it?

Thanks.

How to&Answers:

this works for me

View.inflate(getActivity(), R.layout.dialog, null);

without any warnings nor errors.

Answer:

When inflating a layout for use in a dialog, You can safely pass null here and ignore the warning.

From This Link

The issue here is that AlertDialog.Builder supports a custom view, but
does not provide an implementation of setView() that takes a layout
resource; so you must inflate the XML manually. However, because the
result will go into the dialog, which does not expose its root view
(in fact, it doesn’t exist yet), we do not have access to the eventual
parent of the layout, so we cannot use it for inflation. It turns out,
this is irrelevant, because AlertDialog will erase any LayoutParams on
the layout anyway and replace them with match_parent.

Answer:

Instead of :

inflater.inflate(R.layout.dialog_gallery, null);

do:

inflater.inflate(R.layout.dialog_gallery, parent, false);

Using a @SuppressLint annotation as suggested by Eugen in the comment above might “suppress” the warning, but it doesn’t solve the problem.Using null as an argument for the ViewGroup will cause you problems in the future.

Answer:

Instead of

inflater.inflate(R.layout.dialog_gallery, null);

try

inflater.inflate(R.layout.dialog_gallery, null, false);

It will not attach view to the parent that is null in your case.

See details on Android Reference