“如何修复”;避免将null作为视图根传递;在android中?

“如何修复”;避免将null作为视图根传递;在android中?,android,android-alertdialog,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(d

在我的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();
}
但是我在
View dialoglayout=inflater.inflate(R.layout.dialog\u gallery,null)上得到一个警告
null
下划线

Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's root element)
这意味着什么?我如何修复它


谢谢。

当在对话框中展开布局时,您可以安全地在此处传递null并忽略警告

这里的问题是AlertDialog.Builder支持自定义视图,但是 不提供采用布局的setView()的实现 资源;因此,您必须手动膨胀XML。但是因为 结果将进入对话框,该对话框不显示其根视图 (事实上,它还不存在),我们无法访问最终的 布局的父级,因此我们不能将其用于膨胀。原来,, 这与此无关,因为AlertDialog将删除上的任何LayoutParams 布局仍然存在,并将其替换为match_parent

而不是

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

它不会将视图附加到在您的情况下为空的父视图

有关详细信息,请参见这对我有用

View.inflate(getActivity(), R.layout.dialog, null);
没有任何警告或错误。

而不是:

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


在上面的注释中使用
@SuppressLint注释
可能会“抑制”警告,但这并不能解决问题。使用null作为视图组的参数将在将来导致问题。

在这种情况下,可以传递null。添加一个
@SuppressLint
注释,这样就不会让你感到困扰。这是毫无意义的,因为如果你查看Android的源代码,你会看到这个实现:
公共视图膨胀(int-resource,ViewGroup-root){返回膨胀(resource,root,root!=null);}
。我的意思是,它迟早会产生完全相同的效果。根据我的说法,这是正确的答案,我想使用@SuppressWarnings,但不知道使用什么;有什么想法吗编辑-找到它,@SuppressWarnigns(“InflateParams”)
inflater.inflate(R.layout.dialog_gallery, null);
inflater.inflate(R.layout.dialog_gallery, parent, false);