WindowManager$BadTokenException: Unable to add window


WindowManager$BadTokenException: Unable to add window

 

 

1、http://tech.shantanugoel.com/2010/07/08/badtokenexception-android-dialog-getapplicationcontext.html

logcat输出:

Uncaught handler: thread main exiting due to uncaught exception
android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application

 

这里是这样解释的

It isn’t apparent immediately that what is causing this error. The very first line in the code “Context mContext = getApplicationContext();” is the culprit.

Solution: Just replace “getApplicationContext()” with “this” (i.e. “Context mContext = this;” ) and it will work fine.

Explanation: As to why this is exactly an issue, I’m a bit fuzzy about it myself but this much I’m sure that the contexts that you get with getApplicationContext and this are different. On reading about this function from Android SDK help:

这不是明显的直接的原因引起这个错误。第一行代码“上下文mContext = getApplicationContext();“是罪魁祸首。
  
 解决方案:更换“getApplicationContext()”与“this”(即“上下文mContext =这;”),它会正常工作。
  
 解释:为什么这就是一个问题,我有点模糊自己但这一点我敢肯定,你获得的getApplicationContext上下文是不对的。 阅读有关该函数从Android SDK帮助:

 

2、http://zoulu1.blog.163.com/blog/static/46733149201212711929363/

 

android.view.WindowManager$BadTokenException: Unable to add window – token 
错误提示:android.app.LocalActivityManager$LocalActivityRecord@45f48c40 is not valid; is your activity running? 

今天遇到一个问题,我在地图上添加一个Overlay 然后想在点击这个overlay 图标的时候弹对话框Dialog, 按这样的思路写完后,测试报出了上面的异常。 代码如下:
 
[java] view plain copy
// 退出主程序dialog public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { dialog(); return false; } return super.onKeyDown(keyCode, event); } protected void dialog() { AlertDialog.Builder builder = new Builder(FindwebActivity.this); builder.setMessage(“确定要退出心愿吗?”); builder.setTitle(“提示”); builder.setPositiveButton(“确认”, new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); AgentApp.getInstance().onTerminate(); } }); builder.setNegativeButton(“取消”, new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); }

查资料说问题在于 context. 说不能使用 getApplicationContext(),this 所得到的Context 必须用your_Activity.this 所获取的Context. 而我就是用最后一种的,还是出差。 于是我想下我的实际的运行环境,当前Activity是在tabActivity 中的,那么得到当前的窗体管理者应该是上一级TabActivity 所有,而非我的Activity。 于是我用 getParent()所得到的Context,运行成功。
只需将

 
       

AlertDialog.Builder builder = new Builder(FindwebActivity.this);


//改成
 
       

AlertDialog.Builder builder = new Builder(getParent());


即可



WindowManager$BadTokenException: Unable to add window

 

 

1、http://tech.shantanugoel.com/2010/07/08/badtokenexception-android-dialog-getapplicationcontext.html

logcat输出:

Uncaught handler: thread main exiting due to uncaught exception
android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application

 

这里是这样解释的

It isn’t apparent immediately that what is causing this error. The very first line in the code “Context mContext = getApplicationContext();” is the culprit.

Solution: Just replace “getApplicationContext()” with “this” (i.e. “Context mContext = this;” ) and it will work fine.

Explanation: As to why this is exactly an issue, I’m a bit fuzzy about it myself but this much I’m sure that the contexts that you get with getApplicationContext and this are different. On reading about this function from Android SDK help:

这不是明显的直接的原因引起这个错误。第一行代码“上下文mContext = getApplicationContext();“是罪魁祸首。
  
 解决方案:更换“getApplicationContext()”与“this”(即“上下文mContext =这;”),它会正常工作。
  
 解释:为什么这就是一个问题,我有点模糊自己但这一点我敢肯定,你获得的getApplicationContext上下文是不对的。 阅读有关该函数从Android SDK帮助:

 

2、http://zoulu1.blog.163.com/blog/static/46733149201212711929363/

 

android.view.WindowManager$BadTokenException: Unable to add window – token 
错误提示:android.app.LocalActivityManager$LocalActivityRecord@45f48c40 is not valid; is your activity running? 

今天遇到一个问题,我在地图上添加一个Overlay 然后想在点击这个overlay 图标的时候弹对话框Dialog, 按这样的思路写完后,测试报出了上面的异常。 代码如下:
 
[java] view plain copy
// 退出主程序dialog public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { dialog(); return false; } return super.onKeyDown(keyCode, event); } protected void dialog() { AlertDialog.Builder builder = new Builder(FindwebActivity.this); builder.setMessage(“确定要退出心愿吗?”); builder.setTitle(“提示”); builder.setPositiveButton(“确认”, new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); AgentApp.getInstance().onTerminate(); } }); builder.setNegativeButton(“取消”, new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); }

查资料说问题在于 context. 说不能使用 getApplicationContext(),this 所得到的Context 必须用your_Activity.this 所获取的Context. 而我就是用最后一种的,还是出差。 于是我想下我的实际的运行环境,当前Activity是在tabActivity 中的,那么得到当前的窗体管理者应该是上一级TabActivity 所有,而非我的Activity。 于是我用 getParent()所得到的Context,运行成功。
只需将

 
     

AlertDialog.Builder builder = new Builder(FindwebActivity.this);


//改成
 
     

AlertDialog.Builder builder = new Builder(getParent());


即可


猜你喜欢

转载自blog.csdn.net/hizhangyuping/article/details/80648639