解决API<8时引发的AlerDialog is not created – java.lan...
解决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]
更多相关文章
- 【Android】pendingIntent和Intent
- 关于android中PendingIntent.getBroadcase的注册广播
- Android(安卓)图标外发光
- GPS定位及获取卫星参数实例整理
- Android多个Activity
- Android(安卓)Fragment onActivityResult不执行解决办法
- android 软件开发错误-解决记录
- context.obtainStyledAttributes 研究
- Android之activity跳转并且回调