代码就这么一段:
new AlertDialog.Builder(getApplicationContext(),R.style.MyAlertDialogStyle)
.setTitle("温柔")
.setMessage("不知道 不明了 不想要\n" +
"为什么 我的心")
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.show();

这个时候报错了: Unable to add window -- token null is not for an application

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:576)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:269)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:289)
at android.app.AlertDialog$Builder.show(AlertDialog.java:951)
at cn.bvin.app.androidtest_asgit.MainActivity$1.onClick(MainActivity.java:24)
at android.view.View.performClick(View.java:4446)
at android.view.View$PerformClick.run(View.java:18480)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5294)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:680)
at dalvik.system.NativeStart.main(Native Method)

为什么会这样呢,看一下源码:

 /**
* Create a Dialog window that uses the default dialog frame style.
*
* @param context The Context the Dialog is to run it. In particular, it
* uses the window manager and theme in this context to
* present its UI.
*/
public Dialog(Context context) {
this(context, 0, true);
} /**
* Create a Dialog window that uses a custom dialog style.
*
* @param context The Context in which the Dialog should run. In particular, it
* uses the window manager and theme from this context to
* present its UI.
* @param theme A style resource describing the theme to use for the
* window. See <a href="{@docRoot}guide/topics/resources/available-resources.html#stylesandthemes">Style
* and Theme Resources</a> for more information about defining and using
* styles. This theme is applied on top of the current theme in
* <var>context</var>. If 0, the default dialog theme will be used.
*/
public Dialog(Context context, int theme) {
this(context, theme, true);
} Dialog(Context context, int theme, boolean createContextThemeWrapper) {
if (createContextThemeWrapper) {
if (theme == 0) {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(com.android.internal.R.attr.dialogTheme,
outValue, true);
theme = outValue.resourceId;
}
mContext = new ContextThemeWrapper(context, theme);
} else {
mContext = context;
} mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Window w = PolicyManager.makeNewWindow(mContext);
mWindow = w;
w.setCallback(this);
w.setOnWindowDismissedCallback(this);
w.setWindowManager(mWindowManager, null, null);
w.setGravity(Gravity.CENTER);
mListenersHandler = new ListenersHandler(this);
}

看到上面public的构造方法都是重载下面那个缺省构造方法,三个形参,但是我们看到public的构造方法里最后的createContextThemeWrapper参数都为true

Context context, int theme, boolean createContextThemeWrapper
所以这时mContext的是创建了一个ContextThemeWrapper对象,这下就明白了。 Service,Application,Broadcast都是继承ContextWraper,只有Activity是继承ContextThemeWrapper。
而ContextThemeWrapper又是继承ContextWraper,ContextWraper继承Context。 所以传Activity.this拿到的Context是可以创建ContextThemeWrapper对象的。
再看一下ContextThemeWrapper的源码:
    public ContextThemeWrapper() {
super(null);
} public ContextThemeWrapper(Context base, int themeres) {
super(base);
mThemeResource = themeres;
} @Override protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);
} ... @
Override public void setTheme(int resid) {
mThemeResource = resid;
initializeTheme();
}
ContextThemeWrapper有两个构造函数,一个是不带参的,一个是带一个Context和themeRes的,上面dialog的构造方法中是通过后者来创建的Context。
当然用无构造方法也可以创建ContextThemeWrapper对象,就如上图,attachBaseContext方法可以把Context传递进去,
setTheme则可以把themeRes设置进去。。。

最前沿Android技术分享尽在Android技术分享社,拿起你们的手机打开微信扫一扫,关注我的公众号就给你推荐优秀的知识文章或技术分享了!

												

Android对话框之Context的更多相关文章

  1. Android对话框和帧动画

    Android对话框 在一个例子中展示四种对话框. 设置四个按钮 <LinearLayout xmlns:android="http://schemas.android.com/apk ...

  2. Android对话框(Dialog)

    Android对话框 前几天出差没有进行更新,今天写一下安卓中用的比较多的对话框——AlertDialog. dialog就是一个在屏幕上弹出一个可以让用户做出一个选择,或者输入额外的信息的对话框,一 ...

  3. Android 对话框(Dialog)大全 建立你自己的对话框

    Android 对话框(Dialog)大全 建立你自己的对话框 原文地址: http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html A ...

  4. Android对话框

    这周过的实在是艰辛,自打这周二起我的本本就开始闹"罢工",最后还是重装系统了事. . .   只是可怜了我的那些被格了的软件(悲伤辣么大)!  往事不要再提,人生几度风雨... 简 ...

  5. Android各种获取Context方法

    首先讲一讲这四个函数的区别,后面还有我对context的一些理解区别如下所示: 原文链接http://stackoverflow.com/questions/6854265/getapplicatio ...

  6. Android中的Context(一)

    Android中的Context(一) 在Android开发中,Context可以说是我们接触地非常多的一个概念了,也译作"上下文",但是这个上下文到底是什么却并不好理解. 通俗的 ...

  7. Android获取全局Context的方法

    Android获取全局Context的方法 Android--应用全局获取Context - 超宇的博客 - CSDN博客https://blog.csdn.net/chaoyu168/article ...

  8. Android深入理解Context(二)Activity和Service的Context创建过程

    前言 上一篇文章我们学习了Context关联类和Application Context的创建过程,这一篇我们接着来学习Activity和Service的Context创建过程.需要注意的是,本篇的知识 ...

  9. 绝对让你理解Android中的Context

    这个问题是StackOverFlow上面一个热门的问题What is Context in Android? 整理这篇文章的目的是Context确实是一个非常抽象的东西.我们在项目中随手都会用到它,但 ...

随机推荐

  1. 换个角度说工作单元(Unit Of Work):创建、持有与API调用

    看到一些工作单元的介绍,有两种感觉,第一种是很学院,说了等于没说,我估计很多都是没有自己引入到实际的项目中去,第二种是告诉我一种结果,说这就是工作单元,但是没说为什么要这么使用.所以,本篇想要探讨的是 ...

  2. [计算机图形学] OpenGL读取obj文件并显示其3D效果

     读取三维网格模型(Wavefront OBJ文件) 无法向立方体:cube.obj 有法向兔子模型:bunny.obj 有法向有纹理八字模型:Eight.obj OBJ文件的格式可参考:http: ...

  3. [C++] 几行代码生成漂亮图片,数学家就是牛!

    信息获得处:http://news.cnblogs.com/n/501488/ 分形:http://baike.baidu.com/subview/83243/11213590.htm?fr=alad ...

  4. 使用jQuery和CSS3生成的搜索框变形全屏搜索效果

    在线演示 本地下载 使用jQuery和CSS3过渡变形效果生成的一个搜索框变形效果实现,可以帮助你更好利用页面格式和内容.实验性质的代码,请大家在产品环境里自己修改使用!

  5. Atitit.auto complete 自动完成控件的实现总结

    Atitit.auto complete  自动完成控件的实现总结 1. 框架选型 1 2. 自动完成控件的ioc设置 1 3. Liger  自动完成控件问题 1 4. 官网上的code有问题,不能 ...

  6. Atitit.实现继承的原理and方法java javascript .net c# php ...

    Atitit.实现继承的原理and方法java javascript .net c# php ... 1. 实现继承的问题 1 2. 如何拷贝基类方法?采用prototype原型方式,通过冒充对象 1 ...

  7. atitit. groupby linq的实现(1)-----linq框架选型 java .net php

    atitit.  groupby linq的实现(1)-----linq框架选型 java .net php 实现方式有如下 1. Dsl/ Java8 Streams AP ,对象化的查询api , ...

  8. Django配置与静态文件

    settings.py """ Django settings for myproject project. Generated by 'django-admin sta ...

  9. 爬虫神器xpath的用法(一)

    1.如果你没有安装lxml,请运行pip install lxml或者easy_install lxml安装,如果在安装过程中失败的话, 是因为lxml需要依赖某些库文件,具体可以问下度娘,这里不再赘 ...

  10. linux下IPTABLES配置详解(转)

    如果你的IPTABLES基础知识还不了解,建议先去看看. 开始配置 我们来配置一个filter表的防火墙. (1)查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables - ...