android - 避免将 null 作为 View 根传递(在 AlertDialog 中扩展自定义布局时)

标签 android android-layout android-alertdialog layout-inflater

尝试为 AlertDialog 扩展自定义布局,但不断收到此警告。我已经看到了几种不同的解决方案,但不知道哪个适合我的场景。摆脱此空警告的实际正确方法是什么?

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

@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();
}

最佳答案

您可以尝试使用:

View.inflate(context, R.layout.dialog_customd, null);

关于android - 避免将 null 作为 View 根传递(在 AlertDialog 中扩展自定义布局时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45419680/

相关文章:

android - 对话框中的微调器 - NullPointerException - Android

java - 在 Kindle KDK 中设计 UI(扩展 java.awt)

java - Android 新手 - 无法将示例项目导入 Eclipse

android - 如何通过将值传递给 AlertDialog 的 Intent 来转到下一个 Activity?

java - AlertDialog 不会关闭,需要两次点击才能关闭

android - 如何在 PercentRelativeLayout 中使用 layout_aspectRatio?

java - 如何为 ScrollView 设置 OnClickListener?

android - 无法发现 BLE 设备

java - Android:FragmentTabHost 小部件不显示标题

java - 错误是什么? Android 编程新手