我正在创建自定义提醒对话框
// Biuld the dialog
AlertDialog.Builder alert = new AlertDialog.Builder(this);
// Create the dialog
AlertDialog alertToShow = alert.create();
// Set keyboard to the dialog
alertToShow.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
// Add custom layout to the dialog
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog_view, null);
alert.setView(dialogView);
// Show the dialog
alertToShow.show();
但是从上面我添加自定义布局的下一行
f1.addView(inflater.inflate(R.layout.dialog_view, f1, false));
导致错误
尝试调用虚拟方法' void android.widget.FrameLayout.addView(android.view.View)'在空对象引用上
知道如何修复错误吗?
答案 0 :(得分:0)
您需要在显示对话框后或布局膨胀后找到视图。不是之前
// Add custom layout to the dialog
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog_view, f1, false)
FrameLayout f1 = (FrameLayout) dialogView.findViewById(android.R.id.custom);
f1.addView(dialogView);
// Show the dialog
alertToShow.show();
虽然,我认为你想要alertToShow.setView(dialogView)
,也许
答案 1 :(得分:0)
我必须在创建对话框之前添加视图
// Add custom layout to the dialog
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog_view, null);
alert.setView(dialogView);
// Create the dialog
AlertDialog alertToShow = alert.create();
// Set keyboard to the dialog
alertToShow.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
// Show the dialog
alertToShow.show();