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");
 }
.....
}

 

 

你可能感兴趣的:(我的异常,android)