简体   繁体   English

无法在 Android 中添加 window

[英]Unable to add window in Android

In my dev console, I get the following error:在我的开发控制台中,我收到以下错误:

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@405126b8 is not valid; is your activity running?

It's the follow line: alertDialog = new AlertDialog.Builder(Main.this).create();这是以下行: alertDialog = new AlertDialog.Builder(Main.this).create();

Here is my code:这是我的代码:

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.splashscreen);

        alertDialog = new AlertDialog.Builder(Main.this).create();

        LoadData();
}

I dont no whats wrong.我没有什么错。

You are trying to use a bad context here, try to use the right context.您在这里尝试使用错误的上下文,尝试使用正确的上下文。 See this,看到这个,

Bad token Exception 错误令牌异常

I was seeing this error reported once in a while from some of my apps, and here's what solved it for me:我偶尔会从我的一些应用程序中看到此错误报告,以下是为我解决的问题:

if(!((Activity) context).isFinishing())
{
    //show dialog
}

All the other answers out there seem to be doing weird things like iterating through the list of running activities, but this is much simpler and seems to do the trick.那里的所有其他答案似乎都在做一些奇怪的事情,比如遍历正在运行的活动列表,但这要简单得多,而且似乎可以解决问题。

I don't know if an activity is already considered to be "running" in onCreate.我不知道活动是否已经在 onCreate 中被视为“正在运行”。 Have you tried the same code in onResume()?您是否在 onResume() 中尝试过相同的代码?

Better yet, overwrite onCreateDialog() and later showDialog().更好的是,覆盖 onCreateDialog() 和稍后的 showDialog()。 (see http://developer.android.com/guide/topics/ui/dialogs.html ). (参见http://developer.android.com/guide/topics/ui/dialogs.html )。

Finally, you don't seem to set any properties on the Builder before creating it - no title, no message - maybe one of those is required?最后,在创建之前,您似乎没有在 Builder 上设置任何属性 - 没有标题,没有消息 - 也许其中一个是必需的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM