Android对话框之Context
代码就这么一段:
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的更多相关文章
- Android对话框和帧动画
Android对话框 在一个例子中展示四种对话框. 设置四个按钮 <LinearLayout xmlns:android="http://schemas.android.com/apk ...
- Android对话框(Dialog)
Android对话框 前几天出差没有进行更新,今天写一下安卓中用的比较多的对话框——AlertDialog. dialog就是一个在屏幕上弹出一个可以让用户做出一个选择,或者输入额外的信息的对话框,一 ...
- Android 对话框(Dialog)大全 建立你自己的对话框
Android 对话框(Dialog)大全 建立你自己的对话框 原文地址: http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html A ...
- Android对话框
这周过的实在是艰辛,自打这周二起我的本本就开始闹"罢工",最后还是重装系统了事. . . 只是可怜了我的那些被格了的软件(悲伤辣么大)! 往事不要再提,人生几度风雨... 简 ...
- Android各种获取Context方法
首先讲一讲这四个函数的区别,后面还有我对context的一些理解区别如下所示: 原文链接http://stackoverflow.com/questions/6854265/getapplicatio ...
- Android中的Context(一)
Android中的Context(一) 在Android开发中,Context可以说是我们接触地非常多的一个概念了,也译作"上下文",但是这个上下文到底是什么却并不好理解. 通俗的 ...
- Android获取全局Context的方法
Android获取全局Context的方法 Android--应用全局获取Context - 超宇的博客 - CSDN博客https://blog.csdn.net/chaoyu168/article ...
- Android深入理解Context(二)Activity和Service的Context创建过程
前言 上一篇文章我们学习了Context关联类和Application Context的创建过程,这一篇我们接着来学习Activity和Service的Context创建过程.需要注意的是,本篇的知识 ...
- 绝对让你理解Android中的Context
这个问题是StackOverFlow上面一个热门的问题What is Context in Android? 整理这篇文章的目的是Context确实是一个非常抽象的东西.我们在项目中随手都会用到它,但 ...
随机推荐
- c++如何遍历删除map/vector里面的元素
新技能Get! 问题 对于c++里面的容器, 我们可以使用iterator进行方便的遍历. 但是当我们通过iterator对vector/map等进行修改时, 我们就要小心了, 因为操作往往会导致it ...
- 由于外键的存在引发的一个mysql问题 Cannot change column 'id': used in a foreign key constraint
Duplicate entry ' for key 'PRIMARY' 一查,发现表没有设置自增长. 尝试增加修改表,添加自增长. ALTER TABLE sh_incentive_item MODI ...
- paip.杀不死进程的原因--僵尸进程的解决.txt
paip.杀不死进程的原因--僵尸进程的解决.txt 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn ...
- js程序设计02——变量、作用域问题
首先,ECMAScript中的数据类型分为基本类型.引用类型,基本类型的访问操作是按值的.引用类型的值是保存在内存中的对象,操作对象时,实际上操作的是对象的引用,而非对象自身.“javascript高 ...
- JAVA数组所占内存大小的对比
1.两个数据模型 第一个是基本类型数组,第二个使用的是Float对象数组 public class SummaryModel{ private float[] summaryData; public ...
- spark spark ziliao important
http://book.51cto.com/art/201408/448416.htm 一.如何实现多台机器的ssh无密码登录 当我们在配置多台计算,使之可以相互使用无密码登录-ssh,之前都是一台一 ...
- Linux下面使用rpm命令
RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序” rpm 执行安装包二进制包(Binary)以及源代码包(Source)两种 ...
- 【迁移】—Entity Framework实例详解
好久没有在博客园更新博客了,如今都换了新公司.前段时间写了关于EF迁移的文档,今天拿出来作为这个系列的一篇吧. 一.Entity Framework 迁移命令(get-help EntityFrame ...
- JavaScript日期对象使用总结
javascript Date日期对象的创建 创建一个日期对象: var objDate=new Date([arguments list]); 我总结了参数形式主要有以下3种: new Date(& ...
- 常用js代码
common-function-lib.js /*产生随机颜色*/ function randomColor() { var rand = Math.floor(Math.random() * 0xF ...