如何摆脱“已泄露的窗口”?

时间:2016-11-15 11:46:12

标签: java android progress-bar progressdialog progress

我查看了谷歌,但我找到的所有答案都与我的问题无关......

我的错误日志

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)

我正在做什么,我只是创建进度对话框并且在assynk任务的控制下并且在onPreExecute()对话框中打开,因此在onPostExecute()对话框中正在关闭......

有我的代码

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

有一种方法可以为我提供Dialog实例

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

据我所知,没有什么特别的只是创建对话框,显示它并最终关闭它就是它......

为什么我会收到此错误?

我做错了什么?

修改

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

2 个答案:

答案 0 :(得分:1)

实际上在我的情况下,原因是没有进行中。我试图删除进展和真正的理由揭晓。这是由于OOM在位图解码过程中...但我不知道为什么我在progress dialog中得到了错误日志。

最终我的位图很大,我不得不减少它

我以这种方式完成(如果有人会遇到同样的问题)

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

这意味着我减少了2倍的位图大小,现在应用程序完美无缺!

答案 1 :(得分:0)

尝试使用

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