onCreateDialog 时出错
来源:互联网 发布:石家庄关键字优化公司 编辑:程序博客网 时间:2024/06/07 15:03
在创建各种Dialog时,通过重写Activity其类的方法 protected Dialog onCreateDialog(int id) 来创建Dialog,在需要显示的地方调用showDialog(int id),显示即可。在其中创建Dialog时,总是失败,究其原因:[ERROR/AndroidRuntime(7716): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application] AlertDialog.Builder build = new AlertDialog.Builder(getApplicationContext());这一句遇到问题,后改成 AlertDialog.Builder build = new AlertDialog.Builder(this);问题解决!
Context context = getApplicationContext();
Context context = **Activity.this;
其差别:
getApplicationContext()(SKD 文档):
/**
* Return the context of the single, global Application object of the
* current process. This generally should only be used if you need a
* Context whose lifecycle is separate from the current context, that is
* tied to the lifetime of the process rather than the current component.
*
* <p>Consider for example how this interacts with
* {@ #registerReceiver(BroadcastReceiver, IntentFilter)}:
* <ul>
* <li> <p>If used from an Activity context, the receiver is being registered
* within that activity. This means that you are expected to unregister
* before the activity is done being destroyed; in fact if you do not do
* so, the framework will clean up your leaked registration as it removes
* the activity and log an error. Thus, if you use the Activity context
* to register a receiver that is static (global to the process, not
* associated with an Activity instance) then that registration will be
* removed on you at whatever point the activity you used is destroyed.
* <li> <p>If used from the Context returned here, the receiver is being
* registered with the global state associated with your application. Thus
* it will never be unregistered for you. This is necessary if the receiver
* is associated with static data, not a particular component. However
* using the ApplicationContext elsewhere can easily lead to serious leaks
* if you forget to unregister, unbind, etc.
getApplicationContext() 生命周期是整个应用,应用摧毁它才摧毁
Activity.this的context属于activity ,activity 摧毁他就摧毁
因为只有一个Activity才能添加一个窗体。
- onCreateDialog 时出错
- onCreateDialog onPrepareDialog
- onCreateDialog方法及示例
- onCreateDialog方法及示例
- onCreateDialog方法及示例
- onCreateDialog方法及示例
- onCreateDialog方法及示例
- onCreateDialog方法及示例
- Dialog的使用:onCreateDialog showDialog
- protected Dialog onCreateDialog (int id)
- Activity的onCreateDialog()的问题
- Activity的onCreateDialog方法及示例
- showDialog()、onPrepareDialog()和onCreateDialog()的关系
- showDialog()、onPrepareDialog()和onCreateDialog()的关系 .
- showDialog()、onPrepareDialog()和onCreateDialog()的关系 . .
- showDialog()、onPrepareDialog()和onCreateDialog()的关系 .
- showDialog()、onPrepareDialog()和onCreateDialog()的关系 .
- showDialog(8); public Dialog onCreateDialog(int id)
- dijkstra最短路径算法
- 职场新人 勿做职场5种“鲜花”
- word里面鼠标失效,不并且不能同时打开两个文档怎么办?这样就可以解决。
- Socket编程中的同步、异步、阻塞和非阻塞(转)
- 研究:Android有设计缺陷 可窃取用户数据
- onCreateDialog 时出错
- ADO提速
- java json和object互换
- Jetty vs Tomcat: A Comparative Analysis
- 22个免费的数据可视化和分析工具推荐
- URL字符串相关操作处理
- Axure RP6 学习笔记(2)
- C文件操作
- Xcode快捷键和使用技巧