java - 找不到 "avoid passing null as the view root"的父项

标签 java android layout-inflater

我有这段代码,我在其中定义了自己的 AlertDialog.Builder:

public class UnlockDialog extends AlertDialog.Builder {
    public UnlockDialog(Activity context) {
        super(context);

        LayoutInflater inflater = context.getLayoutInflater();
        View dlgView = inflater.inflate(R.layout.unlock_dialog, null); 
        setView(dlgView);
    }
}

代码工作正常,但我在 inflater.inflate 调用中收到警告:

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

我的适配器也有这个问题,我可以通过提供 parentfalse 来解决它,正如我在这里找到的:Avoid passing null as the view root (need to resolve layout parameters on the inflated layout's root element)

但是,在上述情况下,我似乎没有可用的 parent。我尝试了 context.getParent(),但没有用。

最佳答案

警告不应适用于这种情况。 root 参数,当attachToRoot 为false 时,仅用于调用其generateDefaultLayoutParams() 方法并分配生成的LayoutParams 到膨胀 View 。

在这种情况下,对话框将覆盖它们,因此无论如何它们都不会被使用。

请查看this article ,特别是 Every Rule Has Exception 部分。

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.

关于java - 找不到 "avoid passing null as the view root"的父项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27258686/

相关文章:

android - 如何在 android 中的 gridview 适配器的 getView() 内膨胀另一个布局?

java - 如何在窗口中打开链接?

java - 无法访问 Java 库

java - 检测 Scala 程序中函数变化的最佳实践?

android - eclipse 中的宽度错误

android - 如何更改充气机的 View ?

java - Spring 启动 : Mocking SOAP Web Service

android - OrbotVPN 中的 PortUnreachableException 和 SocketException

java - Android - 定时器类

android - 单击设置为 false [setOutsideTouchable(false)] 外的弹出窗口不起作用