`
貌似掉线
  • 浏览: 256587 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

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

阅读更多
本文原创,转载请保留原文地址: http://maosidiaoxian.iteye.com/blog/1547445

在使用Dialog时,调用dismiss方法,有时会出现异常:java.lang.IllegalArgumentException: View not attached to window manager

出现这个异常的原因可能是,在dismiss对话框的时候,它所在的activity因为一些原因已经先退出了,所以会出现这个异常。
目前我认为最好的解决方法是,使用Activity里面封装好的showDialog(int id)和dismissDialog(int id)方法。
使用示例代码如下(代码取自我的一个项目,去掉与本主题无关内容,如果有小的错误,请自行调试):
/*
 * @(#)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);
	}
}
1
3
分享到:
评论
2 楼 貌似掉线 2012-11-20  
activity里是有这两个方法的。至于 PROGRESS_ID是自己定义的,对话框的一个标识ID。
1 楼 aduovip 2012-11-19  
showDialog(PROGRESS_ID);  

dismissDialog(PROGRESS_ID);  

是怎么调用的呀

相关推荐

Global site tag (gtag.js) - Google Analytics