Sirop4ik
Sirop4ik

Reputation: 5243

How to get rid of "has leaked window"?

I have looked through google but all the answers that I have found does't related to my issue...

There is my error log

Activity com.fittingroom.newtimezone.view.ActivityAcceptNotAccept has leaked window com.android.internal.policy.PhoneWindow$DecorView{57b68a6 V.ED..... R......D 0,0-324,534} that was originally added here
                                                                        at android.view.ViewRootImpl.<init>(ViewRootImpl.java:565)
                                                                        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:326)
                                                                        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
                                                                        at android.app.Dialog.show(Dialog.java:350)
                                                                        at com.fittingroom.newtimezone.view.ActivityAcceptNotAccept.openProgress(ActivityAcceptNotAccept.java:125)
                                                                        at com.fittingroom.newtimezone.presenters.PresenterActivityAcceptNotAccept.openProgress(PresenterActivityAcceptNotAccept.java:125)
                                                                        at com.fittingroom.newtimezone.presenters.PresenterActivityAcceptNotAccept.access$000(PresenterActivityAcceptNotAccept.java:16)
                                                                        at com.fittingroom.newtimezone.presenters.PresenterActivityAcceptNotAccept$1.onPreExecute(PresenterActivityAcceptNotAccept.java:35)
                                                                        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:604)
                                                                        at android.os.AsyncTask.execute(AsyncTask.java:551)
                                                                        at com.fittingroom.newtimezone.presenters.PresenterActivityAcceptNotAccept.setCroppedPreviewImage(PresenterActivityAcceptNotAccept.java:51)
                                                                        at com.fittingroom.newtimezone.view.ActivityAcceptNotAccept.accept(ActivityAcceptNotAccept.java:63)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at android.view.View$DeclaredOnClickListener.onClick(View.java:4735)
                                                                        at android.view.View.performClick(View.java:5697)
                                                                        at android.view.View$PerformClick.run(View.java:22526)
                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:158)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

What I am doing, I just create progress dialog and under controll of assynk task and in onPreExecute() dialog is opening and therefore in onPostExecute() dialog is closing...

There is my code

public final class ActivityAcceptNotAccept extends Activity implements IActivityAcceptNotAccept {

private Dialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_accept_not_accept);

    ...///

    presenter.initView();


}

...///

@Override
public void initView() {
    progressDialog = UtilClass.getDialog(ActivityAcceptNotAccept.this, getString(R.string.loading));
}

public final void accept(View view) {
    switch (count) {
        case firstClick:
            presenter.setCroppedPreviewImage();
            break;
        case secondClick:
            presenter.setResultOkAndFinishActivity();
            break;
    }
}

@Override
public void setDisplaySize() {
    UtilClass.setDisplaySize(getWindowManager().getDefaultDisplay(), getWindow(), 95, 95);
}

@Override
public void openProgress() {
    if (progressDialog != null) {
        progressDialog.show();
    }
}

@Override
public void closeProgress() {
    if (progressDialog != null) {
        progressDialog.cancel();
        progressDialog = null;
    }
}
}

There is a method whicth provide me Dialog instance

public abstract class UtilClass {

public static Dialog getDialog(Context context, String text) {
    final Dialog progressDialog = new Dialog(context);
    progressDialog.setContentView(R.layout.dialog);
    progressDialog.setCanceledOnTouchOutside(false);

    final Window window = progressDialog.getWindow();
    if (window != null) {
        window.setBackgroundDrawableResource(android.R.color.transparent);
    }else {
        Logger.logError("window == null", new NullPointerException(), context);
    }

    final TextView msg = (TextView) progressDialog.findViewById(R.id.id_tv_loadingmsg);
    msg.setText(text);

    return progressDialog;
}

As I can understand there is nothing spesial just create dialog , show it and eventually close it that is it ...

Why I get this error?

What am I doing wrong?

EDIT

AsynkTask

public void setCroppedPreviewImage() {
    new AsyncTask<Void, Void, byte[]>() {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            openProgress();
        }

        @Override
        protected byte[] doInBackground(Void... voids) {
            final String path = model.getImageFilePath();
            final Bitmap croppedBitmap = getCroppedBitmapFromPath(path);
            return getByteArrayFromBitmap(croppedBitmap);
        }

        @Override
        protected void onPostExecute(byte[] result) {
            super.onPostExecute(result);
            closeProgress();
            iActivityAcceptNotAccept.setPreviewImage(result);
        }
    }.execute();
}

Upvotes: 0

Views: 182

Answers (2)

Sirop4ik
Sirop4ik

Reputation: 5243

actually in my case reason was not in progress dialog. I tryed to delete progress and genuine reason unveiled. It was due to OOM during bitmap decode process... But I don't know why I got error log which said that reason in progress dialog .

Eventually my bitmap was to big and I had to reduce it

I accomplished this way(if someone will have the same issue)

final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    final Bitmap bitmap = BitmapFactory.decodeFile(photo.getAbsolutePath(), options);

It means that I reduce bitmap size in 2 times and now app works perfectly!

Upvotes: 1

Gokhan Celikkaya
Gokhan Celikkaya

Reputation: 766

try using

public void closeDialog() { 
    if (progressDialog != null && progressDialog.isShowing()) 
    {
        progressDialog.dismiss();
    }
}
public void displayDialog() { 
    if (progressDialog != null && !progressDialog.isShowing()) 
    {
        progressDialog.show();
    }
}

Upvotes: 0

Related Questions