The specified child already has a parent. You must call removeView() on the child‘s parent first.

    技术2022-07-10  126

    使用自定义对话框AlertDialog出现The specified child already has a parent. You must call removeView() on the child’s parent first.

    分析: 是因为布局被重复加载。

    解决办法: 如果已经有了父容器,就不进行加载 如果没有父容器,就进行加

    private AlertDialog.Builder builder; private AlertDialog dialog; private View view; open_alertdialog.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //一定要判断view是否已经被加载 if (view.getParent() == null) { //加载布局 builder.setView(view); dialog = builder.create(); } //显示布局 dialog.show(); } });

    在onCreate方法中直接进行初始化了

    //创建对话窗 builder = new AlertDialog.Builder(MainActivity.this); //设置对话框的布局 view = View.inflate(MainActivity.this, R.layout.alertdialog_mine, null); open_photo = view.findViewById(R.id.open_photo); open_album = view.findViewById(R.id.open_album);

    我就不想详细写了,如果有问题,请留言询问,我会尽快回复的。

    Processed: 0.011, SQL: 9