如何在android中修复"避免将null作为视图根"?

ome*_*ega 8 android android-alertdialog

在我的Android应用程序中,我创建了一个这样的对话框:

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();
}
Run Code Online (Sandbox Code Playgroud)

但我得到一个View dialoglayout = inflater.inflate(R.layout.dialog_gallery, null);强调警告的警告null.

Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's root element)
Run Code Online (Sandbox Code Playgroud)

这是什么意思,我该如何解决?

谢谢.

Lee*_*dev 11

这对我有用

View.inflate(getActivity(), R.layout.dialog, null);
Run Code Online (Sandbox Code Playgroud)

没有任何警告也没有错误.


Gre*_*nis 8

在对对话框中使用布局进行膨胀时,可以安全地在此处传递null并忽略该警告.

来自此链接

这里的问题是AlertDialog.Builder支持自定义视图,但不提供采用布局资源的setView()实现; 所以你必须手动充气XML.但是,因为结果将进入不公开其根视图的对话框(实际上它还不存在),我们无法访问布局的最终​​父级,因此我们不能将其用于通货膨胀.事实证明,这是无关紧要的,因为AlertDialog无论如何都会擦除布局上的任何LayoutParams并用match_parent替换它们.