解决API<8时引发的AlerDialog is not created - java.lang.IllegalArgumentException: Activity#onCreateDialog did not create a dialog for id 1

报错:java.lang.IllegalArgumentException: Activity#onCreateDialog did not create a dialog for id 4

首先解释下showDialog()和onCreateDialog直接的关系。

api >= 8 时, showDialog(int id,Bundle b) --> onCreateDialog(int id,Bundle b)

api < 8 时,showDialog(int id) ---> onCreateDialog(int id) 。此时,onCreateDialog(int)返回null将报错,具体看代码:
[url]http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/app/Activity.java#Activity.showDialog%28int%29[/url]

结论:
1、要使用showDialog(int id)显示Dialog,就必须重写onCreateDialog(int id)
2、使用showDialog(int id,Bundle b)显示Dialog,可重写onCreateDialog(int id,Bundle b)或onCreateDialog(int id)
3、默认onCreateDialog(int id,Bundle b)调用onCreateDialog(int id)
4、显示Dialog时,若是不需要使用Bundle传参数,可只重写onCreateDialog(int id)

其它参考:
[url]http://stackoverflow.com/questions/4225526/android-runtimeexception-oncreatedialog-did-not-create-a-dialog-for-id[/url]

页: [1] 查看完整版本: 解决API<8时引发的AlerDialog is not created - java.lang.IllegalArgumentException: Activity#onCreateDialog did not create a dialog for id 1

更多相关文章

  1. 【Android】pendingIntent和Intent
  2. 关于android中PendingIntent.getBroadcase的注册广播
  3. Android(安卓)图标外发光
  4. GPS定位及获取卫星参数实例整理
  5. Android多个Activity
  6. Android(安卓)Fragment onActivityResult不执行解决办法
  7. android 软件开发错误-解决记录
  8. context.obtainStyledAttributes 研究
  9. Android之activity跳转并且回调

随机推荐

  1. 为什么学习Python?使用哪个系统比较好?
  2. Netscaler添加首页下载连接
  3. Kubernetes Pod驱逐策略
  4. Kubernetes Pod水平自动伸缩(HPA)
  5. git常用命令
  6. OpenKruise v0.8.0 版本发布:K8s 社区首个
  7. Linux运维入门教程01-02 (UNIX和Linux操
  8. 冲着这份大数据分析案例,我立马下载该分析
  9. 掌握这8个Java实战小技巧,再也不用为了内
  10. Prometheus警报规则