Android窗口WindowLeaked - 活动已泄露最初添加到此处的窗口

时间:2015-06-29 05:09:25

标签: android multithreading

我尝试在线程旁边显示ProgressDialog。但是当应用程序运行Progressdialog时会出现这种异常

  

android.view.WindowLeaked:   活动com.testApp.CaptureSignature已泄露窗口   com.android.internal.policy.impl.PhoneWindow $ {DecorView 528dd504   V.E ..... R .....我。最初添加的0,0-949,480}

行执行时

错误获取

pDialog.show();

public void syncing(final int sel){

        if(sel==1){
            ProgressDialo pDialog = new ProgressDialog(CaptureSignature.this);
            pDialog.setMax(100);
            pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pDialog.setCancelable(false);
            pDialog.setProgress(0);
            pDialog.setOnDismissListener(new MyCustomDialog.OnDismissListener() {
                @Override
                public void onDismiss(final DialogInterface dialog) {
                    doaftersync(pDialog.getProgress(),sel);

                }

            }); 
            pDialog.setMessage("Syncing Deliveries.Please wait..");
            pDialog.show();
            Thread  background = new Thread (new Runnable() {
                   public void run() {

                       progressHandler.sendMessage(progressHandler.obtainMessage());


                       int stat = deliveryup();
                        if(stat==1){

                            try {
                                locationManager.removeUpdates(locationListner);
                            } catch (Exception e2) {
                            }

                        }else{
                            pDialog.dismiss();
                            return;

                        }

                        progressHandler.sendMessage(progressHandler.obtainMessage());
                         int isustat=issueup();
                            if(isustat==0){
                                pDialog.dismiss();
                                return;

                            }

                        progressHandler.sendMessage(progressHandler.obtainMessage());

                        int locstat=locationup();
                            if(locstat==0){
                                pDialog.dismiss();
                                return;

                            }

                        cleanup();

                        progressHandler.sendMessage(progressHandler.obtainMessage());
                        pDialog.dismiss();
                        return;
                   }
                });
             background.start();

        }

    }

    // handler for the background updating
    Handler progressHandler = new Handler() {
        public void handleMessage(Message msg) {
            pDialog.incrementProgressBy(25);
        }
    };

任何帮助.. !!

3 个答案:

答案 0 :(得分:3)

使用ProgressDialog或使用Handler方法取消主线程中的runOnUiThread()

也许你得到了异常,因为当你的Activity被销毁时,进度对话框正在运行。你应该在Activity被销毁时解雇对话

答案 1 :(得分:2)

在UI线程中执行所有UI操作。

runOnUiThread(new Runnable() {

        @Override
        public void run() {
            pDialog.dismiss();
        }
    });

答案 2 :(得分:1)

我认为安全的方式:

    if(dialog.isShowing()){
        dialog.dismiss();
    }