android.view.WindowLeaked

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11590382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:10:09  来源:igfitidea点击:

android.view.WindowLeaked

android

提问by pengwang

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

it give me :

它给了我:

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.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.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)

回答by Ajith Memana

Window leaked exceptions are usually caused by dialogs which are not dismissed properly. ie if you are planning to dismiss a dialog in Onpostexecute of asynctask and the activity that created it has ended it will throw a window leak. Make sure you dimisss dialog in onPause of the activity.

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

回答by ABI

Whenever you initiate a ProgressDialog, that should be dismissed properly after the background task gets done, or even cancellation of the background running task. So,

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

instead of mProgressBar.hide();use mProgressBar.dismiss();

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

you will not get android.view.WindowLeaked error

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

hope this helps

希望这可以帮助

回答by Rana Ranvijay Singh

Check if you are using finish() function before the mDialog.show() function. If it is remove the finish() and add it after the show().

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

回答by Sir Codesalot

Another scenario where this error occurs is when the app crushes while a dialog is shown. Look above this error for additional errors.

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

回答by Hiren Patel

whren you miss this code : mProgressDialog.dismis();that time may be you will receive this type of error.

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

回答by Reejesh PK

In onStop()or onDestroy()callback, dismiss the dialog using dialogName.dismiss();

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

Also do a null check. That is:

也做一个空检查。那是:

if (dialogName != null) {
    dialogName.dismiss();
}

回答by Arul Mani

The problem is AsyncTask.

问题是AsyncTask

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

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

You create a progress bar inside async task which makes a problem when you move to another activity.

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

For this before dismiss or show()

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

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

Usage of isFinishing()

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.

回答by hiddeneyes02

The problem for me was happening because I used the android:noHistory="true"for the activity that was causing the problem, and it only happened on Android 6.0 API 23.

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

回答by Matt Mendrala

Simply dismissing the dialog was not enough to get rid of the error for me. It turns out my code was holding onto a reference to the dialog even after it was dismissed. The key for me was to set that reference to null after dismissing the dialog.

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