android.view.WindowManager$BadTokenException: Unable to add window — token null

比较懒的写博客,这篇文章也放在草稿箱很久了,无意间翻出来,发现自己现在异常类的问题基本上都不用Google上搜了,一般android studio 的logcat中都会给出很详细的错误描述.项目中遇到问题都能够解决,感觉什么都会又有很多的东西要学,很多知识不熟悉,容易记混淆.

所以还是用博客写一写成长的过程吧.

 

  1. private Context mcontext;  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {mcontext = getApplicationContext();  
  5.         System.out.println("mcontext=" + mcontext);  
  6.    }  
  7.  
  8.  new AlertDialog.Builder(mcontext)  
  9.             .setIcon(android.R.drawable.ic_dialog_alert)  
  10.             .setTitle("Warnning")  
  11.             .setMessage(  
  12.                     "You forget to write the message. Do you want to fill out it ??")  
  13.             .setPositiveButton("Yes", positiveListener).setNegativeButton(  
  14.                     "No", negativeListener).create().show();  

出现android.view.WindowManager$BadTokenException: Unable to add window — token null这个异常的原因提示已经说得很清楚了,

导致报这个错是在于new AlertDialog.Builder(mcontext),虽然这里的参数是AlertDialog.Builder(Context context)但我们不能使用getApplicationContext()获得的Context,而必须使用Activity,因为只有一个Activity才能添加一个窗体。 

解决方法:将new AlertDialog.Builder(Context context)中的参数用Activity.this(Activity是你的Activity的名称)来填充就可以正确的创建一个Dialog了。 

正确的使用姿势:

 

  1. new AlertDialog.Builder(MyActivity.this)  
  2.                 .setIcon(android.R.drawable.ic_dialog_alert)  
  3.                 .setTitle("Warnning")  
  4.                 .setMessage(  
  5.                         "You forget to write the message. Do you want to fill out it ??")  
  6.                 .setPositiveButton("Yes", positiveListener).setNegativeButton(  
  7.                         "No", negativeListener).create().show(); 

 

系统源码报错的位置在ViewRootImpl中的setView中:

public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) { 

  ......

 switch (res) {
    case WindowManagerGlobal.ADD_BAD_APP_TOKEN:
    case WindowManagerGlobal.ADD_BAD_SUBWINDOW_TOKEN:
        throw new WindowManager.BadTokenException(
                "Unable to add window -- token " + attrs.token
                + " is not valid; is your activity running?");
    case WindowManagerGlobal.ADD_NOT_APP_TOKEN:
        throw new WindowManager.BadTokenException(
                "Unable to add window -- token " + attrs.token
                + " is not for an application");
    case WindowManagerGlobal.ADD_APP_EXITING:
        throw new WindowManager.BadTokenException(
                "Unable to add window -- app for token " + attrs.token
                + " is exiting");
    case WindowManagerGlobal.ADD_DUPLICATE_ADD:
        throw new WindowManager.BadTokenException(
                "Unable to add window -- window " + mWindow
                + " has already been added");
    case WindowManagerGlobal.ADD_STARTING_NOT_NEEDED:
        // Silently ignore -- we would have just removed it
        // right away, anyway.
        return;
    case WindowManagerGlobal.ADD_MULTIPLE_SINGLETON:
        throw new WindowManager.BadTokenException("Unable to add window "
                + mWindow + " -- another window of type "
                + mWindowAttributes.type + " already exists");
    case WindowManagerGlobal.ADD_PERMISSION_DENIED:
        throw new WindowManager.BadTokenException("Unable to add window "
                + mWindow + " -- permission denied for window type "
                + mWindowAttributes.type);
    case WindowManagerGlobal.ADD_INVALID_DISPLAY:
        throw new WindowManager.InvalidDisplayException("Unable to add window "
                + mWindow + " -- the specified display can not be found");
    case WindowManagerGlobal.ADD_INVALID_TYPE:
        throw new WindowManager.InvalidDisplayException("Unable to add window "
                + mWindow + " -- the specified window type "
                + mWindowAttributes.type + " is not valid");
 }
.....
}

 

 

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值