我将LinearLayout扩展到AlertDialog中。它工作正常,但是如果我将其删除并再次启动,则会出现以下错误
java.lang.IllegalStateException:指定的子代已经有一个父代。您必须先在孩子的父母上调用removeView()。
在contructor中:
infoWindow = (LinearLayout) ((Activity) context).getLayoutInflater().inflate(R.layout.map_info_content, null);
之后:
protected boolean onTap(int index) {
Builder dialog = new AlertDialog.Builder(context);
dialog.setView(infoWindow);
dialog.show();
return true;
}
我试图将对话框保留在内存中并将其视图设置为null,但无法解决问题
将膨胀视图添加到父级后,您将无法在Android中重复使用它们ViewGroup
。在您身边,您正在尝试在对话框中重复使用膨胀视图。要解决此问题,请在AlertDialog类本身中扩大视图。
我建议您为此创建一个单独的类。该onCreateDialog
方法如下所示:
public Dialog onCreateDialog(Bundle savedInstance){
// create a new dialog builder
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
// dialog view to inflate
View view = getActivity().getLayoutInflater()
.inflate(R.layout.map_info_content, null);
// sets the view of our dialog
builder.setView(view);
// create and return the dialog
return builder.create();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句