hash
hash

Reputation: 5406

Android window WindowLeaked - Activity has leaked window that was originally added here

im try to show ProgressDialog in side the thread.but when the app run Progressdialog will crach and it give this Exception

android.view.WindowLeaked: Activity com.testApp.CaptureSignature has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{528dd504 V.E..... R.....I. 0,0-949,480} that was originally added here

error getting when line executing

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);
        }
    };

Any Help .. !!

Upvotes: 2

Views: 4792

Answers (3)

Rajesh Jadav
Rajesh Jadav

Reputation: 12861

Dismiss Your ProgressDialog in Main Thread Using Handler or Using runOnUiThread() Method

Maybe You get exception because Progress dialog is running while Your Activity is destroyed. you should dismiss dialog when Activity is destroyed

Upvotes: 4

CodeMonster
CodeMonster

Reputation: 300

I think the safe way :

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

Upvotes: 1

Emil
Emil

Reputation: 2806

Do all your UI actions in a UI thread.

runOnUiThread(new Runnable() {

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

Upvotes: 2

Related Questions