Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 280
omega Avatar asked Dec 26 '14 22:12

omega


3 Answers

this works for me

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

without any warnings nor errors.

like image 97
Leebeedev Avatar answered Nov 11 '22 09:11

Leebeedev


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.

like image 8
Greg Ennis Avatar answered Nov 11 '22 09:11

Greg Ennis


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.

like image 2
Ojonugwa Jude Ochalifu Avatar answered Nov 11 '22 07:11

Ojonugwa Jude Ochalifu