Android 错误:视图未附加到窗口管理器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3378102/
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 09:58:04  来源:igfitidea点击:

Error: View not attached to window manager

android

提问by jax

I am getting the following remotely from clients so I don't know what hardware etc they are using.

我从客户那里远程获取以下信息,所以我不知道他们正在使用什么硬件等。

java.lang.IllegalArgumentException: View not attached to window manager
       at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
       at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
       at android.view.Window$LocalWindowManager.removeView(Window.java:417)
       at android.app.Dialog.dismissDialog(Dialog.java:279)
       at android.app.Dialog.access
    progressDialog = new ProgressDialog( this );
    progressDialog.setMessage(getString(R.string.get_ready));
    progressDialog.setCancelable(false);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.setMax(12);
    progressDialog.show();

    new CountDownTimer(3000, 250) {

         @Override
        public void onTick(long millisUntilFinished) {
             //progressDialog.incrementProgressBy(1);
         }

         @Override
        public void onFinish() {
             progressDialog.dismiss(); //********* ERROR HAPPENS HERE *********
             nextQuestion();
         }
    }.start();
0(Dialog.java:72) at android.app.Dialog.run(Dialog.java:108) at android.app.Dialog.dismiss(Dialog.java:263) at com..mysite.android.ActivityGame.onFinish(ActivityGame.java:154) at android.os.CountDownTimer.handleMessage(CountDownTimer.java:118) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4203) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)

This is happening because of a ProgressDialog

这是因为 ProgressDialog

<activity android:theme="@style/GameTheme" android:name=".ActivityGame" android:screenOrientation="portrait" android:launchMode="singleTask" android:configChanges="keyboardHidden|orientation"></activity>

The Activity looks like this in the Manifest.

活动在清单中看起来像这样。

if (pDialog.isShowing()) {
   pDialog.cancel();
}

So what could this mean? I think it has something to do with the Activity being destroyed then created but as you can see I have the configChanges set correctly.

那么这意味着什么呢?我认为这与 Activity 被销毁然后创建有关,但正如您所看到的,我的 configChanges 设置正确。

回答by Ajay

Try:

尝试:

public void onDestroy(){
super.onDestroy();
if(progressDialog!=null)
if(progressDialog.isShowing()){
progressDialog.cancel();
}

}

in your overridden onDestroy()or onStop()methods.

在您的重写onDestroy()onStop()方法中。

回答by Shoaib Ahmed

This problem arises when trying to show a dialog after you've exited an Activity.

在退出Activity.

I just solved this problem just by writing down the following code:

我只是通过写下以下代码解决了这个问题:

if (pDialog.getWindowToken() != null)
{
   pDialog.dismiss();
}

Basically, from which class you started progressDialog, override onDestroy()method and do this way. It solved Activity has leaked windowproblem.

基本上,从哪个类开始progressDialog,覆盖onDestroy()方法并这样做。它解决了Activity 已泄漏窗口问题。

回答by Fedor

It usually happens when you call dismiss after activity has been closed.

它通常发生在您在活动关闭后调用关闭时。

回答by Joel Teply

In order to handle invisible views, you cannot use isShowing() and should check for the window attachment itself in your onDestroy()

为了处理不可见的视图,您不能使用 isShowing() 并且应该在 onDestroy() 中检查窗口附件本身

if (test_service_overlay != null) {
     if (test_service_overlay.getWindowToken() != null) {
         WindowManager windowManager = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);
         windowManager.removeViewImmediate(test_service_overlay);
     }
}

or in my case, I was not using a dialog but a custom window added by the WindowManager, which was (possibly) invisible.

或者就我而言,我没有使用对话框,而是使用 WindowManager 添加的自定义窗口,该窗口(可能)不可见。

    if(pDialog.isShowing())
    {
      try
      {
        pDialog.dismiss();
      }
      catch(Exception e) {// nothing }

    }

回答by Shahul3D

Im also faced the same problem when I tried to dismiss the dialog at onPageFinished method of Web view. sometimes onPageFinished called after the activity has been closed.

当我尝试在 Web 视图的 onPageFinished 方法中关闭对话框时,我也遇到了同样的问题。有时在活动关闭后调用 onPageFinished。

Here is the solution for it:

这是它的解决方案:

##代码##

Try it!

尝试一下!

回答by oskarko

You can ask "dialog.isIndeterminate()" as well. Works fine.

您也可以询问“dialog.isIndeterminate()”。工作正常。