8

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.

omega
  • 40,311
  • 81
  • 251
  • 474

4 Answers4

13

this works for me

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

without any warnings nor errors.

Leebeedev
  • 2,126
  • 22
  • 31
8

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.

Greg Ennis
  • 14,917
  • 2
  • 69
  • 74
  • I'd like to use @SuppressWarnings for this but don't know what to use; any ideas? - edit - Found it, @SuppressWarnigns("InflateParams") – linuxgnuru Mar 29 '17 at 10:10
2

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.

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
0

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

ivkil
  • 414
  • 1
  • 4
  • 11
  • 1
    This is meaningless, because if you look the Android's source code you'll see this implementation: `public View inflate(int resource, ViewGroup root) { return inflate(resource, root, root != null); }`. I mean, sooner or later it will do the same effect exactly. – Joaquin Iurchuk Jul 27 '15 at 18:32