<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 (
[email protected])
*/
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>