执行doInBackground方法后,如何使“警报”对话框在Async中正常工作

时间:2017-06-19 11:02:07

标签: android

             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)
  

我必须在点击之前确认订单(书籍:方法)预订您的订单按钮。我正在超越异常。我如何处理此异常?请帮助我克服此问题。谢谢提前。

2 个答案:

答案 0 :(得分:1)

在启动另一个Activity之前取消当前的alertDialog。调用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();


        }

答案 1 :(得分:1)

您还需要发布doInBackground方法中的代码。您可以在该方法中以某种方式退出您的活动,因此尝试从&#34; mContext&#34;创建一个AlertDialog。 (无论如何,显示其余代码)会导致窗口泄漏。

这个问题可能符合您的要求:Activity has leaked window that was originally added

问题可能是您需要在更改/离开活动之前解除()您的AlertDialog,或者您需要在创建活动之前确保活动处于活动状态。