解决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] 

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

你可能感兴趣的:(android,android,问题)