Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android在AlertDialog之后关闭ProgressDialog_Android_Android Alertdialog_Progressdialog_Dismiss - Fatal编程技术网

Android在AlertDialog之后关闭ProgressDialog

Android在AlertDialog之后关闭ProgressDialog,android,android-alertdialog,progressdialog,dismiss,Android,Android Alertdialog,Progressdialog,Dismiss,我试图在用户单击确认按钮后将POST方法发送到服务器,但在单击并获得响应后,ProgressDialog仍在运行 paymentSubmitBtn = (Button)findViewById(R.id.paymentSubmitBtn); paymentSubmitBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) {

我试图在用户单击确认按钮后将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();
    }

我建议将对话框对象传递给sendTransactionInfo或使ProgressDialog对话框全局化。只要简单地看一下您的代码,它看起来可能是一个范围问题。

Hmmm请尝试进行更改

alert.dismiss();

但是让它成为全球性的..对AlertDialog的引用

我不太明白你的问题。。。是否要在按下对话框中的“确定”时取消该对话框?否我想在收到服务器响应时取消该对话框,但您正在使用单击处理程序创建新警报。在某个地方有一个全局对话对象。单击处理程序ist中声明的dialoog在结尾处不可访问。如果这是范围问题,那么不会出现编译时错误吗?我猜它使用的是DialogInterface对话框,这就是为什么它不会取消ProgressDialog。当ProgressDialog对象未被传递并且是onClick的本地对象时,其他方法如何访问该对象?当我更改DialogInterface参数的名称时,问题得到了解决。