android ProgressDialo异常 no dialog with id 0 was ever shown via Activity#showDialog
2012-03-14 21:07
309 查看
http://byandby.iteye.com/blog/1133327
屏幕中央有一个ProgressDialog 提示正在验证账户,验证的过程是通过一个异步任务来完成的,但是如果这时我不停的点击Back键--》一定要不停的点,就会抛出一个异常,异常见下图。
![](http://dl.iteye.com/upload/attachment/525086/d2713fd6-fd09-39fa-8f74-c3ef1e0b22e8.png)
这个异常的意思就是说没有一个
id为0的dialog 已经通过 showDialog()方法显示了。出现这个异常的原因是:当我们点击Back键的时候ProgressDialog 其实已经被结束了,它在当前的那个 Activity已经不存在了,然而后台的AsyncTask任务其实还在继续执行 。。然后我们又在onPostExecute() 方法取消显示这个Dialog,但是 这个时候 这个dialog已经没了啊,因为当我们点击Back键的时候 就会结束那个Dialog!! 找不到它了 所以抛出了这个异常。
解决办法就是 设置那个Dialog的onCancel事件,并且在onCancel方法里边判断 如果那个AsyncTask任务对象不等于null ,就结束它,代码片段如下。
Java代码
![](http://byandby.iteye.com/images/icon_star.png)
private AccountVerifyAsyncTask mAccountVerifyTask;
Java代码
![](http://byandby.iteye.com/images/icon_star.png)
if(mAccountVerifyTask == null || mAccountVerifyTask.getStatus() == AsyncTask.Status.FINISHED || mAccountVerifyTask.isCancelled()){
mAccountVerifyTask = (AccountVerifyAsyncTask) new AccountVerifyAsyncTask().execute(username,password);
}
Java代码
![](http://byandby.iteye.com/images/icon_star.png)
class AccountVerifyAsyncTask extends AsyncTask<String ,Integer, Integer>{
@Override
protected void onPreExecute() {
showDialog(VERIFY_DIALOG_KEY);
}
@Override
protected Integer doInBackground(String... params) {
String username = params[0];
String password = params[1];
int result = AccountManager.instance().accountVerify(username, password);
return result;
}
@Override
protected void onPostExecute(Integer result) {
dismissDialog(VERIFY_DIALOG_KEY);
int code = result;
AccountManager.instance().handleVerifyResult(code);
if(AccountManager.SUCCESS == code){
AccountManager.QUERY_CANCELED = false;
onAccountChanged();
}
}
}
Java代码
![](http://byandby.iteye.com/images/icon_star.png)
switch(id){
case VERIFY_DIALOG_KEY: {
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage(getResources().getString(R.string.account_verify_account));
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
if(mAccountVerifyTask != null){
mAccountVerifyTask.cancel(true);
}
}
});
//mVerifyDialog = dialog;
return dialog;
}
屏幕中央有一个ProgressDialog 提示正在验证账户,验证的过程是通过一个异步任务来完成的,但是如果这时我不停的点击Back键--》一定要不停的点,就会抛出一个异常,异常见下图。
![](http://dl.iteye.com/upload/attachment/525086/d2713fd6-fd09-39fa-8f74-c3ef1e0b22e8.png)
这个异常的意思就是说没有一个
id为0的dialog 已经通过 showDialog()方法显示了。出现这个异常的原因是:当我们点击Back键的时候ProgressDialog 其实已经被结束了,它在当前的那个 Activity已经不存在了,然而后台的AsyncTask任务其实还在继续执行 。。然后我们又在onPostExecute() 方法取消显示这个Dialog,但是 这个时候 这个dialog已经没了啊,因为当我们点击Back键的时候 就会结束那个Dialog!! 找不到它了 所以抛出了这个异常。
解决办法就是 设置那个Dialog的onCancel事件,并且在onCancel方法里边判断 如果那个AsyncTask任务对象不等于null ,就结束它,代码片段如下。
Java代码
![](http://byandby.iteye.com/images/icon_star.png)
private AccountVerifyAsyncTask mAccountVerifyTask;
Java代码
![](http://byandby.iteye.com/images/icon_star.png)
if(mAccountVerifyTask == null || mAccountVerifyTask.getStatus() == AsyncTask.Status.FINISHED || mAccountVerifyTask.isCancelled()){
mAccountVerifyTask = (AccountVerifyAsyncTask) new AccountVerifyAsyncTask().execute(username,password);
}
Java代码
![](http://byandby.iteye.com/images/icon_star.png)
class AccountVerifyAsyncTask extends AsyncTask<String ,Integer, Integer>{
@Override
protected void onPreExecute() {
showDialog(VERIFY_DIALOG_KEY);
}
@Override
protected Integer doInBackground(String... params) {
String username = params[0];
String password = params[1];
int result = AccountManager.instance().accountVerify(username, password);
return result;
}
@Override
protected void onPostExecute(Integer result) {
dismissDialog(VERIFY_DIALOG_KEY);
int code = result;
AccountManager.instance().handleVerifyResult(code);
if(AccountManager.SUCCESS == code){
AccountManager.QUERY_CANCELED = false;
onAccountChanged();
}
}
}
Java代码
![](http://byandby.iteye.com/images/icon_star.png)
switch(id){
case VERIFY_DIALOG_KEY: {
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage(getResources().getString(R.string.account_verify_account));
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
if(mAccountVerifyTask != null){
mAccountVerifyTask.cancel(true);
}
}
});
//mVerifyDialog = dialog;
return dialog;
}
相关文章推荐
- java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog
- no lable views point to this text field with an android:lablFor=”@+id/@+id
- Android中showDialog(int id)开发小记
- No label views point to this text field with an android:labelFor="@+id/@+id/***" attribute
- Android布局问题: No resource found that matches the given name (at 'layout_above' with value @id/tv)
- no lable views point to this text field with an android:lablFor=”@+id/@+id
- HwLauncher﹕ Launcher dialog dismiss failed : java.lang.IllegalArgumentException: no dialog with id 1
- The method showDialog(int) from the type Activity is deprecated-android旧版dialog不能用的问题
- android No enclosing instance of type BasicActivity is accessible. Must qualify the allocation with
- android不依赖具体activity弹出Dialog对话框,即全局性对话框 (
- Android Studio: Plugin with id 'android-library' not found
- 解决导入项目提示Plugin with id 'com.github.dcendents.android-maven' not found.
- Android通过共享用户ID来实现多Activity进程共享
- 解决Android: You need to use a Theme.AppCompat theme (or descendant) with this activity.
- AndroidStudio遇到Gradle sync failed: Plugin with id 'com.android.application' not found.
- onResume() called after onSaveInstanceState in FragmentActivity causing DialogFragmet.show() to cras
- Android通过共享用户ID(shareUserId)来实现多个Activity进程共享
- 【Android策略】Can not perform this action after onSaveInstanceState with DialogFragment
- 有关android中“Activity伪装”的Dialog问题的解决
- No mapping found for HTTP request with URI [/Test/findbyid] in DispatcherServlet