代码就这么一段:
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. NBIbatis 基础框架

    基础框架 NBIbatis 为真实在用的系统中剥离出的一个ibatis.net应用框架,目的在于通过此项目让软件工程师集中关注表现层及业务规则编写. 通过数据访问和业务规则可快速搭建不同表现形式的网站 ...

  2. servlet servlet基本概念和helloservlet实例

    java web目录结构: web.xml是重要配置文件,容器正是通过这个文件找到service方法. 用纯文本+tomcat组装简单servlet web 新建HelloServlet.java: ...

  3. paip.提升性能---mysql 性能 测试以及 参数调整.txt

    paip.提升性能---mysql 性能 测试以及 参数调整.txt 作者Attilax  艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://b ...

  4. Vue.js进阶

    <!-- 动态Props --><!DOCTYPE html> <html lang='utf-8'> <head> <meta charset= ...

  5. Android SQLite3工具常用命令行总结

    Android SDK的tools目录下提供了一个sqlite3.exe工具,这是一个简单的sqlite数据库管理工具.开发者可以方便的使用其对sqlite数据库进行命令行的操作. 程序运行生成的*. ...

  6. python解析json

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 引用 import json 编码:把一个Python对象编码转换成Json字符串 json.dumps ...

  7. Memcached常规应用与分布式部署方案

    1.Memcached常规应用 $mc = new Memcache(); $mc->conncet('127.0.0.1', 11211); $sql = sprintf("SELE ...

  8. python多线程生成缩略图

    在img目录下7张图片 分别是 11.jpg 22.jpg 33.jpg 44.jpg 55.jpg 66.jpg 77.jpg #encoding=utf-8 import os import ti ...

  9. UIButton的titleEdgeInsets和imageEdgeInsets属性

    转:http://www.cnblogs.com/huichun/p/3419596.html uiButton控件上自带了一个uiLabel类型的子控件和一个uiImageView类型的子控件,如果 ...

  10. 手机APP和WAP版的区别

    一.APP 1.APP安装后可以在手机桌面显示 2.APP可以调用系统硬件如:摄像头,拨号.定位.打印等等. 3.APP可以调用其它APP,比如支付宝.微信等等. 4.APP可以存在系统服务中,可以有 ...