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, 按这样的思路写完后,测试报出了上面的异常。 代码如下:
 
// 退出主程序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());

即可
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值