如何在安卓中修复“避免将null作为视图根传递”的问题?

8
在我的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_gallery, null); 这一行代码中,我收到了一个警告,标记着 null

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

这是什么意思,如何解决这个问题?谢谢。

1
https://dev59.com/_GAf5IYBdhLWcg3wSQ4z - Al Lelopath
https://dev59.com/_18d5IYBdhLWcg3w1E50 - Al Lelopath
1
在这种情况下,传递null是可以的。添加一个@SuppressLint注释,这样就不会再出现问题了。 - Eugen Pechanec
4个回答

13

这对我有效

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

没有任何警告或错误。


这是根据https://dev59.com/_18d5IYBdhLWcg3w1E50#26596164的正确答案。 - Bugs Happen

8
在为对话框填充布局时,您可以安全地传递 null 并忽略警告。
来自此链接
问题在于 AlertDialog.Builder 支持自定义视图,但不提供接受布局资源的 setView() 实现;因此,必须手动膨胀 XML。但是,由于结果将进入对话框,而对话框不公开其根视图(实际上,它尚不存在),因此我们无法使用它进行膨胀。事实证明,这是无关紧要的,因为 AlertDialog 会擦除布局上的任何 LayoutParams,并将其替换为 match_parent。

我想在这里使用 @SuppressWarnings,但不知道该用什么;有什么建议吗?
  • 编辑 - 找到了,@SuppressWarnings("InflateParams")
- linuxgnuru

2

改为:

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

做:

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

正如上方评论中的 Eugen 建议的那样,使用 @SuppressLint注释 可能会“抑制”警告,但这并不能解决问题。在 ViewGroup 中使用 null 作为参数将会在未来给你带来麻烦。


0

而不是

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

尝试

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

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

请参见Android参考上的详细信息。


1
这是没有意义的,因为如果你查看Android的源代码,你会看到这个实现:public View inflate(int resource, ViewGroup root) { return inflate(resource, root, root != null); }。我的意思是,迟早它会产生完全相同的效果。 - Joaquin Iurchuk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接