简体   繁体   中英

How to remove view from AlertDialog Android

I am making an ALert Dialog with custom EditText Field.

I made a View variable and then associated it with my custom EditText field.

requestView = inflater.inflate(R.layout.send_request,null);

Then I added that view to my AlertDialog

alert.setView(requestView);

And after that I added the onClick Method To My Button to perform the alert Dialog action..

chatRequestbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                alert.setPositiveButton("Send", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        request = requestMsg.getText().toString();

                        send();

                    }
                });
                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();

                    }
                });

                alert.show();

            }
        });

It worked correctly. But after pressing cancel option on alert dialog when I press the button again to perform the alert dialog option.

It crashes with the following error.

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                                                                           at android.view.ViewGroup.addViewInner(ViewGroup.java:4417)
                                                                           at android.view.ViewGroup.addView(ViewGroup.java:4258)
                                                                           at android.view.ViewGroup.addView(ViewGroup.java:4230)
                                                                           at android.support.v7.app.AlertController.setupCustomContent(AlertController.java:647)
                                                                           at android.support.v7.app.AlertController.setupView(AlertController.java:463)
                                                                           at android.support.v7.app.AlertController.installContent(AlertController.java:226)
                                                                           at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:257)
                                                                           at android.app.Dialog.dispatchOnCreate(Dialog.java:395)
                                                                           at android.app.Dialog.show(Dialog.java:294)
                                                                           at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:955)
                                                                           at com.buckydroid.anonchat.User$3.onClick(User.java:86)
                                                                           at android.view.View.performClick(View.java:5637)
                                                                           at android.view.View$PerformClick.run(View.java:22433)
                                                                           at android.os.Handler.handleCallback(Handler.java:751)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6126)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

I though making the view null and adding the view again while clicking on the button will fix the issue. But same problem again and again..

Your problem is here:

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(requestView);

In this case alert is not the dialog, but a builder. So every time when you're trying to show it - it rebuilds this dialog and trying to add same view for it - requestView . Because it is cached in the builder. To fix it - move

requestView = inflater.inflate(R.layout.send_request,null);
alert.setView(requestView);

to your OnClick method where you're showing the dialog. So it should look like this:

chatRequestbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              requestView = inflater.inflate(R.layout.send_request,null);
              alert.setView(requestView);

                alert.setPositiveButton("Send", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        request = requestMsg.getText().toString();

                        send();

                    }
                });
                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();

                    }
                });

                alert.show();

            }
        });

If you want to use an existing View, use this.

alert.setOnDismissListener(new OnDismissListener(){
    ((ViewGroup)requestView.getParent()).removeView(requestView);
});

I converted the above code from Kotlin to Java by hand, plz check before use.

You want to set an on dismiss listener. I did something like: The dialog should set an on Dismiss listener

alert.setOnDismissListener(new DialogInterface.OnDismissListener()  {
       @Override
       public void onDismiss(DialogInterface dialog) {
              ((ViewGroup)requestView.getParent()).removeView(requestView);
       }
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM