避免将null作为视图根传递(在AlertDialog中扩大自定义布局时)

Mac*_*ver 4 android android-layout layout-inflater android-alertdialog

试图为AlertDialog添加自定义布局,但请注意这一点。我已经看到了几种不同的解决方案,但是不知道哪种方案对我来说是正确的。摆脱此空警告的实际正确方法是什么?

避免将null用作视图根(需要解析膨胀的布局的根元素上的布局参数)

@Override
public void onClick(View v) {
  AlertDialog alertDialog = new 
  AlertDialog.Builder(getActivity()).create();

  LayoutInflater inflater = getActivity().getLayoutInflater();
  View content = inflater.inflate(R.layout.dialog_customd, null);
  alertDialog.setView(content);

  alertDialog.show();
}
Run Code Online (Sandbox Code Playgroud)

小智 6

您可以尝试使用:

View.inflate(context, R.layout.dialog_customd, null);
Run Code Online (Sandbox Code Playgroud)


zee*_*han 4

像这样做:

View content = inflater.inflate(R.layout.dialog_customd, parent, false);
Run Code Online (Sandbox Code Playgroud)