AlertDialog requestFeature() must be called before adding content问题
原创
©著作权归作者所有:来自51CTO博客作者WongKyunban的原创作品,请联系作者获取转载授权,否则将追究法律责任
将alert.setCentView()、 alertDialog.getWindow()放到alertDialog.show()后边
View myView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_alert, null, false);
AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
// 将alert.setCentView(xx); alertDialog.getWindow();放到alertDialog.show();后边
// 否则,你会收到requestFeature() must be called before adding content这样的错误
alertDialog.show();
alertDialog.setContentView(myView);
((TextView) myView.findViewById(R.id.tv_alert)).setText("您的信息已收到!");
TextView submitBtn = (TextView) myView.findViewById(R.id.tv_alert_submit);
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
或者在show之前调用AlertDialog.Builder.setView(),如下所示;
new AlertDialog.Builder(getActivity())
.setView(myView)
.setMessage("这是一条测试消息")
.setPositiveButton("确定", null)
.setNegativeButton("取消", null)
.show();
谢谢阅读!