简体   繁体   中英

Is it impossible to put another popup in a popup message via builder in android?

I almost finished building an app using android studio. However, I noticed some problems in the testing process.

Here is a brief summary of what I'm doing:

  1. When you click 'button', a popup message appears.
  2. The popup message has two buttons. Pressing the first button only ends the popup message. When you press the second button, the current popup message is terminated and additional popup messages appear.
  3. The second popup message has a button and an 'EditText' area for entering the email address.
  4. When you click the button, the popup message ends with performing the Javascript function using the email address entered in the EditText area.

First of all, clicking on the two buttons and entering an email address works normally. But when I do this again for the second time, the app is terminating unexpectedly. I think the 'Builder' part I used to implement the popup message is incorrectly nested, but I can not figure out which part is the problem.

Each pop-up message is implemented using 'Builder' as shown below.

final AlertDialog.Builder builder;  //for first popup message
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert);} 
    else {
    builder = new AlertDialog.Builder(context);}

final AlertDialog.Builder builder2;  //for second popup message
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    builder2 = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert);} 
    else {
    builder2 = new AlertDialog.Builder(context);}

Now the full popup message implementation code. Sorry for the difficulty of reading the code.

button_makingpopup.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (User.getInstance() != null) {
            builder.setCancelable(false)    //first popup setting
                    .setMessage("Do you want to email the measurement data??")
                    .setPositiveButton("Save and Send", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();    //first popup disappear

                            builder2.setCancelable(false)   //second popup setting
                                    .setMessage("Enter your email address below.")
                                    .setPositiveButton("Send", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            final String email_popup = input.getText().toString();
                                            WebView view2 = new WebView(context);
                                            view2.getSettings().setJavaScriptEnabled(true);
                                            view2.getSettings().setDomStorageEnabled(true);

                                            view2.setWebViewClient(new WebViewClient() {
                                                public void onPageFinished(WebView view, String url) {
                                                    view.loadUrl("javascript:(function() { //some javascript function})()");
                                                    }
                                                });
                                            dialog.cancel();    //second popup disappear
                                            }
                                        });
                            AlertDialog alert_send = builder2.create();
                            alert_send.show();  //second popup appear
                        }
                    })
                    .setNegativeButton("Save only", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();    //first popup disappear
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();   //first popup appear
            }
        else
        //Non-question parts

I would appreciate your advice.

environment: android studio 2.3.3

error message "java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first."

I solved this problem by handling the view. I refer to this. @kasgoku

Thank you also for those who gave me advice by the comment.

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