-3
             public void book(View c)
              {

                      DoLogin doLogin = new DoLogin();
                      doLogin.execute("");

                         finish();
                  }
            public class DoLogin extends AsyncTask<String, String, String> {


    String Docdate = docdate.getText().toString().trim();
    String Docpre = docprefix.getText().toString().trim();
    String Cname = cname.getSelectedItem().toString().trim();
    String Product = pname.getSelectedItem().toString().trim();
    String Dest = dest.getSelectedItem().toString().trim();
    String Dname = dname.getSelectedItem().toString().trim();
    String Qty = qty.getText().toString().trim();
    String Remarks = remarks.getText().toString().trim();
    String Duedt = duedt.getText().toString().trim();


    @Override
    protected void onPreExecute() {

    }

    @Override
    protected void onPostExecute(String r) {
        Toast.makeText(OrderEntry.this, r, Toast.LENGTH_SHORT).show();

        if (isSuccess) {



                final AlertDialog.Builder alertDialogBuilder = new 
                      AlertDialog.Builder(mContext);

                alertDialogBuilder.setMessage("Would You like to Confirm 
                  Your Order?");
                alertDialogBuilder.setPositiveButton("yes",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface arg0, int 
                                arg1) {
                                try {
                                    rs1 = stmt.executeQuery(Qr);
                                    if (rs1.next()) {
                                        Toast.makeText(mContext, 
                          "hiiiiiiiiiiiiii", Toast.LENGTH_SHORT).show();
                                        Intent i = new 
                          Intent(OrderEntry.this, OrderRequest.class);

                                        startActivity(i);
                                    } else {


                                        Toast.makeText(mContext, 
                      "byeeeeeeeeeeeee", Toast.LENGTH_SHORT).show();
                                    }
                                } catch (Exception v) {

                                    Log.e("insert", v + "");

                                }
                            }
                        });

                alertDialogBuilder.setNegativeButton("No", new 
                DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.cancel();
                        isSuccess1 = false;
                    }
                });

                alertDialog = alertDialogBuilder.create();
                alertDialog.setCanceledOnTouchOutside(false);
                alertDialog.show();


            }



                  }

                  protected String doInBackground(String... params) {
                       try {

            Connection conn = (Connection) connectionClass.CONN();
            if (conn == null) {
                z = "Error in connection with SQL server";
            } else {

                Qr = "insert into [Dealer].[dbo].[BookingOrder] 

              (DocDate,DocPrefix,CustomerName,Destination,
                DealerName,Productname,Quantity,Remarks,DueDate)
             values('" + Docdate + "','" + Docpre + "','" + Cname + "',
                   '" + Dest + "','" + Dname + "','" + Product + "','"

                  + Qty  + "','" + Remarks + "','" + Duedt + "')";
                       stmt = conn.createStatement();
                       z = "confirmation";
                     isSuccess = true;
                       }


                    } catch (Exception ex) {
                        isSuccess = false;
                        Log.e("con", ex + "");

                     }

                      }
                      return z;
                   }
                }





   > LOGCAT ERRORS:
         /WindowManager: android.view.WindowLeaked: Activity 
              com.example.admin.dealerdemo.OrderEntry has leaked window 
                  com.android.internal.policy.PhoneWindow$DecorView{dba0674 
             V.E...... R.....I. 0,0-639,188} that was originally added here
                 at android.view.ViewRootImpl.<init>(ViewRootImpl.java:368)
                 at 
      android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:299)
                 at 

           android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
                 at android.app.Dialog.show(Dialog.java:319)
                 at 


                  com.example.admin.dealerdemo.OrderEntry$DoLogin$override.
                 onPostExecute(OrderEntry.java:295)
                 at 

                   com.example.admin.dealerdemo.OrderEntry$DoLogin$override.
                    access$dispatch(OrderEntry.java)
                 at com.example.admin.dealerdemo.OrderEntry$DoLogin.
                onPostExecute(OrderEntry.java:0)
                 at com.example.admin.dealerdemo.OrderEntry$DoLogin
                              .onPostExecute(OrderEntry.java:231)
                 at android.os.AsyncTask.finish(AsyncTask.java:651)
                 at android.os.AsyncTask.-wrap1(AsyncTask.java)
                 at 
      android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
                 at android.os.Handler.dispatchMessage(Handler.java:102)
                 at android.os.Looper.loop(Looper.java:148)
                 at 
          android.app.ActivityThread.main(ActivityThread.java:5417)
                 at java.lang.reflect.Method.invoke(Native Method)
                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.
                run(ZygoteInit.java:726)
                 at 
              com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I have to Confirm the order before clicking on (book :Method)Book your Order button.I am getting above exception.How can I handle this exception ?Please help me to overcome this problem.Thanks in Advance.

Devika
  • 41
  • 6
  • 1
    Also its duplicate question. Google it before asking this question. https://stackoverflow.com/questions/2850573/activity-has-leaked-window-that-was-originally-added – Rameshbabu Jun 19 '17 at 11:06
  • Just dismiss your visible dialogue like this if(alertDialogBuilder != null) alertDialogBuilder.dismiss(); then start you new activity – Rameshbabu Jun 19 '17 at 11:08
  • Already I checked it,but it does not working – Devika Jun 19 '17 at 11:10
  • Hi its an duplicate question and read the answers carefully and solve it. – Rameshbabu Jun 19 '17 at 11:12
  • Possible duplicate of [Activity has leaked window that was originally added](https://stackoverflow.com/questions/2850573/activity-has-leaked-window-that-was-originally-added) – Saurabh Bhandari Jun 19 '17 at 12:09

2 Answers2

1

Cancel the current alertDialog before start another Activity. call alertDialog.dismiss()

@Override
protected void onPostExecute(String r) {
    Toast.makeText(OrderEntry.this, r, Toast.LENGTH_SHORT).show();

    if (isSuccess) {



            final AlertDialog.Builder alertDialogBuilder = new 
                  AlertDialog.Builder(mContext);

            alertDialogBuilder.setMessage("Would You like to Confirm 
              Your Order?");
            alertDialogBuilder.setPositiveButton("yes",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface arg0, int 
                            arg1) {
                            try {

                                  alertDialog.dismiss();
                                rs1 = stmt.executeQuery(Qr);
                                if (rs1.next()) {
                                    Toast.makeText(mContext, 
                      "hiiiiiiiiiiiiii", Toast.LENGTH_SHORT).show();
                                    Intent i = new 
                      Intent(OrderEntry.this, OrderRequest.class);

                                    startActivity(i);
                                } else {
                                   alertDialog.dismiss();

                                    Toast.makeText(mContext, 
                  "byeeeeeeeeeeeee", Toast.LENGTH_SHORT).show();
                                }
                            } catch (Exception v) {

                                Log.e("insert", v + "");

                            }
                        }
                    });

            alertDialogBuilder.setNegativeButton("No", new 
            DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    alertDialog.dismiss();
                    isSuccess1 = false;
                }
            });

            alertDialog = alertDialogBuilder.create();
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.show();


        }
sasikumar
  • 12,540
  • 3
  • 28
  • 48
1

You need to post the code from your doInBackground method as well. It is possible that your activity is somehow exited during that method, so trying to create an AlertDialog from "mContext" (whatever that is, show the rest of your code) causes a window leak.

This other question may have what you need: Activity has leaked window that was originally added

The problem is likely that you need to dismiss() your AlertDialog before changing/leaving activities, or you need to make sure the activity is active before you create it.

Jack F.
  • 154
  • 1
  • 1
  • 10