【问题标题】:Android dismiss ProgressDialog after AlertDialogAndroid 在 AlertDialog 之后关闭 ProgressDialog
【发布时间】:2014-05-08 14:19:52
【问题描述】:

我正在尝试在用户单击确认按钮后向服务器发送 POST 方法,但单击后我得到响应,ProgressDialog 仍在运行。

paymentSubmitBtn = (Button)findViewById(R.id.paymentSubmitBtn);
paymentSubmitBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                final String receiver = receiverMymoIdEdtTxt.getText().toString();
                runOnUiThread(new Runnable() {
                    public void run() {
                        AlertDialog.Builder builder = new AlertDialog.Builder(Payment.this);
                        builder.setTitle(R.string.message);
                        builder.setMessage("Transaction Details")
                               .setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                                   public void onClick(DialogInterface dialog, int id) 
                                   {
                                      ProgressDialog dialog = ProgressDialog.show(Payment.this, "", "Connect to server..." , true);
                                       sendTransactionInfo(receiver);
                                   }
                               })
                               .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                                   public void onClick(DialogInterface dialog, int which) { 
                                   }
                                });                    
                        AlertDialog alert = builder.create();
                        alert.show();               
                    }
                });

            }
        });

这是我用来发送 POST 方法并尝试关闭 ProgressDialog 的函数。

protected void sendTransactionInfo(final String receiver)
    {
        Thread t = new Thread() {

            public void run() {
                Looper.prepare(); //For Preparing Message Pool for the child Thread
                HttpClient client = new DefaultHttpClient();
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); 
                HttpResponse response;

                String URL = baseURL + "/api-create-transaction";

                try {
                        HttpPost post = new HttpPost(URL);

                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                        nameValuePairs.add(new BasicNameValuePair("section", "API" ));

                        nameValuePairs.add(new BasicNameValuePair("receiver", receiver ));          

                        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                        response = client.execute(post);

                        if(response!=null)
                        {
                            String res = EntityUtils.toString(response.getEntity());

                            final JSONObject result = new JSONObject(res);

                            String operation = result.getString("operation");

                            if(operation.trim().equalsIgnoreCase("success"))
                            {                               
                                JSONObject data = result.getJSONObject("data");
                            }
                            else if(operation.trim().equalsIgnoreCase("error"))
                            {

                                runOnUiThread(new Runnable() {
                                    public void run() {
                                        AlertDialog.Builder builder = new AlertDialog.Builder(Payment.this);
                                        builder.setTitle(R.string.message);
                                        try 
                                        {
                                            builder.setMessage(result.getString("message"))
                                                   .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                                       public void onClick(DialogInterface dialog, int id) {
                                                          dialog.dismiss();
                                                       }
                                                   });
                                        } catch (JSONException e) {

                                            e.printStackTrace();
                                        }                      
                                        AlertDialog alert = builder.create();
                                        alert.show();               
                                    }
                                });
                                Toast.makeText(getBaseContext(), result.getString("message") , Toast.LENGTH_LONG).show();                           
                            }
                            dialog.dismiss();
                        }

                } catch(final Exception e) {

                    runOnUiThread(new Runnable() {
                        public void run() {
                            AlertDialog.Builder builder = new AlertDialog.Builder(Payment.this);
                            builder.setTitle(R.string.message);
                            builder.setMessage(e.toString())
                                   .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                       public void onClick(DialogInterface dialog, int id) {
                                           dialog.dismiss();
                                       }
                                   });                     
                            AlertDialog alert = builder.create();
                            alert.show();               
                        }
                    });

                }

                Looper.loop();
            }
        };
        t.start();
    }

【问题讨论】:

  • 我不太明白你的问题......你想在对话框中按下确定时取消对话框吗?
  • 不,我想在收到服务器响应时取消对话框
  • 但是您正在使用点击处理程序创建一个新警报。在某处有一个全局对话对象。在点击处理程序中声明的对话框最终无法访问。

标签: android android-alertdialog progressdialog dismiss


【解决方案1】:

我的建议是将您的对话框对象传递给您的 sendTransactionInfo() 或将 ProgressDialog 对话框设为全局。看起来这可能是一个范围问题(只是简单地看一下您的代码)。

【讨论】:

  • 如果这是范围问题,那么不会出现编译时错误吗?
  • 我猜它正在使用 DialogInterface 对话框,这就是它不关闭 ProgressDialog 的原因。当 ProgressDialog 对象没有被传递并且它是 onClick 的本地对象时,其他方法怎么能访问它?
  • 当我更改 DialogInterface 参数的名称时问题解决了。
【解决方案2】:

嗯,试试这个换一下

alert.dismiss();

但要让它成为全局的..AlertDialog 的引用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多