Dialog为何只能用Activity的Context
如果使用非Activity的context来创建Dialog,会报异常:
WindowManager$BadTokenException: Unable to add window -- token null is not for an application
原因如下:
- 使用不同的context来获取的WindowManager是不一样的
mWindowManager = context.getSystemService(Context.WINDOW_SERVICE)
如果上述context是Application,实际调用的是ContextImpl.getSystemService,获取的WM是new WindowManagerImpl(display);即mParentWindow为null;
如果上述context是Activity,获取的WM是new WindowM