The specified child already has a parent. You must call removeView的解决办法

事情是这样子的,我在一个活动中自定义了一个AlertDialog,通过一个按钮点击即可弹出,而后来出现的情况是,第一次点击就没问题,

正常跳出,而第二次就直接程序闪退,然后报标题中的错误,

这是我的AlertDialog的代码,和布局

final AlertDialog setTheOrder = new AlertDialog.Builder(Passenger.this)
                .setView(R.layout.order_layout)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//以下都是这个Dialog布局中的一些控件
                        getLocation("湘潭",startInput.getText().toString(),0);
                        getLocation("湘潭",endInput.getText().toString(),1);
                        startPlace = startInput.getText().toString();
                        endPlace = endInput.getText().toString();
                        startTime = getFormatTime(startTimeInput_h.getText().toString(),startTimeInput_m.getText().toString());
                        endTime = getFormatTime(endTimeInput_h.getText().toString(),endTimeInput_m.getText().toString());
                        supplyCar = getSupplyCar(canSupplyCar.getText().toString());
                       
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        
                    }
                }).create();
        setTheOrder.show();

再来看这个错误,字面意思呢,就是说这个特定的child已经有了一个父布局,必须移除这个布局,才能继续你的内容,经过一番查找,一个子类View,要使用必须依赖于父类View,如果要使用子类的View,必须先调用父类的removeView的方法才能保证子类的使用(不知道表述的对不对,错了烦请大佬们指正)

好了,既然大概的思路有了,我们先得得到一个父类对象,我的解决办法是,通过活动的onCreate方法里面setContentView里面那个布局,调用

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.order_layout, null);

这两个方法得到父类对象,然后使用完这个Dialog之后,调用removeAllViews,在上面AlertDialog的两个button里面的最后,调用这个方法

final AlertDialog setTheOrder = new AlertDialog.Builder(Passenger.this)
                .setView(parent)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        getLocation("湘潭",startInput.getText().toString(),0);
                        getLocation("湘潭",endInput.getText().toString(),1);
                        startPlace = startInput.getText().toString();
                        endPlace = endInput.getText().toString();
                        startTime = getFormatTime(startTimeInput_h.getText().toString(),startTimeInput_m.getText().toString());
                        endTime = getFormatTime(endTimeInput_h.getText().toString(),endTimeInput_m.getText().toString());
                        supplyCar = getSupplyCar(canSupplyCar.getText().toString());
                        parent.removeAllViews();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        parent.removeAllViews();
                    }
                }).create();
        setTheOrder.show();

我这里是规定了只能点这两个按钮才能结束这个Dialog,其实如果设置了点击Dialog之外的屏幕区域也可以退出Dialog的话,在相应的方法里面也要调用

猜你喜欢

转载自www.cnblogs.com/Yintianhao/p/9461979.html