34
inviteBu.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(ChoiceList.size()>0)
             {

                    LayoutInflater factory = LayoutInflater.from(MobileConnectActivity.this);
                    final View textEntryView = factory.inflate(R.layout.invite_dialog, null);
                    final EditText et =(EditText) textEntryView.findViewById(R.id.usercontent_edit);

                   dia= new AlertDialog.Builder(MobileConnectActivity.this)
                        .setTitle(getString(R.string.invite_input_content))
                        .setView(textEntryView)
                        .setPositiveButton(getString(R.string.invite_send), new DialogInterface.OnClickListener() {
                            public void onClick(final DialogInterface dialog, int whichButton) {
                                dialog.dismiss();
                                 if(et.getText().toString()==null && et.getText().equals("") )
                                {
                                    Toast.makeText(getApplicationContext(), getString(R.string.invite_content_check), Toast.LENGTH_SHORT).show();
                                }
                                 else{


                                     new AsyncTask<Void, Void, String>() {

                                         CustomDialog mProgressBar = new CustomDialog(MobileConnectActivity.this, R.style.dialog);

                                        protected void onPreExecute() {

                                            mProgressBar.show();
                                        };

                                        protected void onCancelled() {
                                            mProgressBar.hide();
                                        };

                                        @Override
                                        protected String doInBackground(Void... params) {

                                                ChoiceList=removeDuplicateList(ChoiceList);
                                                for(int i=0;i<ChoiceList.size();i++)
                                                {
                                                    Log.i("aaa",""+ChoiceList.get(i));
                                                    sendSMS(ChoiceList.get(i), et.getText().toString());
                                                }
                                            return "OK";
                                        }

                                        protected void onPostExecute(String response) {
                                            mProgressBar.hide();

                                            if (response != null ) {

                                                 Toast.makeText(getApplicationContext(), getString(R.string.invite_succeed), Toast.LENGTH_SHORT).show();
                                                 Intent intent = new Intent();
                                                    intent.setClass(MobileConnectActivity.this, inviteMainActivity.class);
                                                    startActivity(intent);

                                                MobileConnectActivity.this.finish();
                                            } else {
                                                //mHelper.showResponseErrorMessage(response);
                                                Intent intent = new Intent();
                                                intent.setClass(MobileConnectActivity.this, inviteMainActivity.class);
                                                startActivity(intent);
                                                finish();
                                            }

                                        };

                                    }.execute();

                                 }

                            }
                        })
                        .setNegativeButton(getString(R.string.invite_cancel), new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {

                                /* User clicked cancel so do some stuff */
                            }
                        }).show();

             }
            else
            {
                Toast.makeText(getApplicationContext(), getString(R.string.invite_choice_check), Toast.LENGTH_SHORT).show();
            }
        }
    });

它给了我:

07-21 03:36:24.519: E/WindowManager(23240): Activity com.portaura.myaura.invite.MobileConnectActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405c32d0 that was originally added here
07-21 03:36:24.519: E/WindowManager(23240): android.view.WindowLeaked: Activity com.portaura.myaura.invite.MobileConnectActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405c32d0 that was originally added here
07-21 03:36:24.519: E/WindowManager(23240):     at android.view.ViewRoot.<init>(ViewRoot.java:266)
07-21 03:36:24.519: E/WindowManager(23240):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:174)
07-21 03:36:24.519: E/WindowManager(23240):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:117)
07-21 03:36:24.519: E/WindowManager(23240):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
07-21 03:36:24.519: E/WindowManager(23240):     at android.app.Dialog.show(Dialog.java:241)
07-21 03:36:24.519: E/WindowManager(23240):     at com.portaura.myaura.invite.MobileConnectActivity$3$1$1.onPreExecute(MobileConnectActivity.java:161)
07-21 03:36:24.519: E/WindowManager(23240):     at android.os.AsyncTask.execute(AsyncTask.java:391)
07-21 03:36:24.519: E/WindowManager(23240):     at com.portaura.myaura.invite.MobileConnectActivity$3$1.onClick(MobileConnectActivity.java:201)
07-21 03:36:24.519: E/WindowManager(23240):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:165)
07-21 03:36:24.519: E/WindowManager(23240):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 03:36:24.519: E/WindowManager(23240):     at android.os.Looper.loop(Looper.java:130)
07-21 03:36:24.519: E/WindowManager(23240):     at java.lang.reflect.Method.invokeNative(Native Method)
07-21 03:36:24.519: E/WindowManager(23240):     at java.lang.reflect.Method.invoke(Method.java:507)
4

9 回答 9

96

窗口泄漏异常通常是由未正确关闭的对话框引起的。即,如果您打算在 asynctask 的 Onpostexecute 中关闭一个对话框并且创建它的活动已经结束,它将引发窗口泄漏。确保在活动的 onPause 中关闭对话框。

于 2013-01-03T17:21:26.630 回答
15

每当您启动 ProgressDialog 时,应该在后台任务完成后正确关闭,甚至取消后台运行任务。所以,

而不是mProgressBar.hide();使用mProgressBar.dismiss();

你不会得到 android.view.WindowLeaked 错误

希望这可以帮助

于 2016-02-25T07:39:48.077 回答
10

检查您是否在 mDialog.show() 函数之前使用了 finish() 函数。如果是删除 finish() 并在 show() 之后添加它。

于 2013-12-26T12:38:12.720 回答
6

当您错过此代码时:mProgressDialog.dismis();那时您可能会收到此类错误。

于 2013-07-12T12:35:56.413 回答
5

onStop()onDestroy()回调中,使用关闭对话框dialogName.dismiss();

还要进行空检查。那是:

if (dialogName != null) {
    dialogName.dismiss();
}
于 2019-03-31T07:52:36.093 回答
5

发生此错误的另一种情况是应用程序在显示对话框时崩溃。查看此错误上方的其他错误。

于 2018-04-17T13:28:52.490 回答
2

问题是AsyncTask

new AsyncTask<Void, Void, String>() {

    CustomDialog mProgressBar = new CustomDialog(MobileConnectActivity.this, R.style.dialog);
    .... 
}

您在异步任务中创建了一个进度条,当您移动到另一个活动时会出现问题。

为此,在解雇或 show() 之前

if(!isFinishing() && dialog != null) { dialog.dismiss();}

isFinishing()的用法

 * Check to see whether this activity is in the process of finishing,
 * either because you called {@link #finish} on it or someone else
 * has requested that it finished.  This is often used in
 * {@link #onPause} to determine whether the activity is simply pausing or
 * completely finishing.
 *
 * @return If the activity is finishing, returns true; else returns false.
于 2019-11-10T07:21:37.147 回答
0

对我来说问题正在发生,因为我使用了android:noHistory="true"导致问题的活动,并且它只发生在 Android 6.0 API 23 上。

于 2019-07-25T15:32:56.907 回答
0

简单地关闭对话框并不足以消除我的错误。事实证明,即使在对话框被解除后,我的代码仍保留对对话框的引用。对我来说,关键是在关闭对话框后将该引用设置为 null。

于 2018-03-11T17:42:46.893 回答