Dialog调用dismiss方法出现异常解决方法

<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40"><b>Dialog调用dismiss方法出现异常解决方法</b><br><br>本文原创,转载请保留原文地址: http://maosidiaoxian.iteye.com/blog/1547445
<br />
<br />在使用Dialog时,调用dismiss方法,有时会出现异常:java.lang.IllegalArgumentException: View not attached to window manager
<br />
<br />出现这个异常的原因可能是,在dismiss对话框的时候,它所在的activity因为一些原因已经先退出了,所以会出现这个异常。
<br />目前我认为最好的解决方法是,使用Activity里面封装好的showDialog(int id)和dismissDialog(int id)方法。
<br />使用示例代码如下(代码取自我的一个项目,去掉与本主题无关内容,如果有小的错误,请自行调试):<pre class="java" name="code">/*
 * @(#)SearchActivity.java       Project:lol
 * Date:2012-4-29
 *
 * Copyright (c) 2011 CFuture09, Institute of Software, 
 * Guangdong Ocean University, Zhanjiang, GuangDong, China.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sinaapp.msdxblog.android.lol.activity;


import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.LinearLayout;


import com.sinaapp.msdxblog.android.lol.R;
import com.sinaapp.msdxblog.androidkit.thread.HandlerFactory;


/**
 * @author Geek_Soledad (66704238@51uc.com)
 */
public abstract class WebViewActivity extends Activity {


protected WebView mSearchWV;
protected Context mContext;
private static final int PROGRESS_ID = 1;


/**
* 返回需要加载的URL地址。

* @return 需要加载的URL地址。
*/
protected abstract String getHomeUrl();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
mSearchWV = new WebView(mContext);
mSearchWV.getSettings().setJavaScriptEnabled(true);
mSearchWV.setWebViewClient(new WebViewClient() {


public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}


@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
showDialog(PROGRESS_ID);
}


@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
dismissDialog(PROGRESS_ID);
}
});
addContentView(mSearchWV, new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));


mSearchWV.loadUrl(getHomeUrl());
}


@Override
protected Dialog onCreateDialog(int id) {
if (id == PROGRESS_ID) {
return ProgressDialog.show(mContext, null,
mContext.getString(R.string.loading));
}
return super.onCreateDialog(id);
}
}
</pre></html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值