最近遇到一个奇葩的问题,好郁闷 之前也没有仔细看。问题偶尔出现一次。再去查看日志时,出现

view.WindowManager$BadTokenException: Unable to add window…is not valid; is your activity running?

什么情况,activity is runing? 对呀,activity没有destory呀,在跑呢,怎么会出现 对话框 附加到activity上加不了呢,还说无效的,似是activity被destory了呢。

后面一一检查 代码,确实 存在 activity 被destory时,异常还没有捕获到,而异步的AsynTask 还在运行呢。之后异常被捕获到了,再用对话框显示错误时,就会报错了,activity都死了,对话框还能活呀,再说此时提示已经没有意义了。应该丢掉 放弃 后续的处理。

那么 其实只需要 在异常 任务中 加上 isFinishing() 判断下,若activity挂了,就不管了。...

     new AsyncTask<Void, Void, Object>(){
boolean isNoNetException = false;
boolean isNetLagException = false; //网络超时等不稳定 异常
boolean isConnException = false; //连接掉了,需要重新登录
@Override
protected void onPreExecute() {
//TODO 清空掉 所有的已填写的数据
pd = new ProgressDialog(SCInfoGainActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setMessage(getString(R.string.pop_window_waiting_submit_tip));
pd.show();
} @Override
protected Object doInBackground(Void... params) {
DataService ds = new DataService(SCInfoGainActivity.this);
Object retVal = null;
try {
retVal = ds.persubmitServiceChangeInfo(checkPhoneCustInfo.getUserId(), servicenum, checkPhoneCustInfo.getNettype(), serviceLists);
}catch (HessianConnectionException e) {
if(!DeviceUtil.isConnect(SCInfoGainActivity.this)){
//没网络发生的异常
isNoNetException = true;
}else{
//网络掉线了,请重新
isNetLagException = true;
}
}catch (SocketTimeoutException e) {
L.v(SCInfoGainActivity.class.getSimpleName(), "SocketTimeoutException ... "+e.getLocalizedMessage());
if(!DeviceUtil.isConnect(SCInfoGainActivity.this)){
isNoNetException = true; //没网络发生的异常
}else{
isNetLagException = true; //网络掉线了不稳定,请重新
}
e.printStackTrace();
}catch (ConnectException e){
L.v(SCInfoGainActivity.class.getSimpleName(), "SocketTimeoutException ... "+e.getLocalizedMessage());
if(!DeviceUtil.isConnect(SCInfoGainActivity.this)){
isNoNetException = true; //没网络发生的异常
}else{
isConnException = true; //网络掉线了不稳定,请重新
}
}catch (Exception e) {
//不是连接上的异常,比较复杂 有各种服务端返回的异常信息....
e.printStackTrace();
}
return retVal;
} @Override
protected void onPostExecute(Object result) {
if(isFinishing()) return ; //若activity 都没有了,没有必要弹对话框了
pd.dismiss();
if(isNoNetException){
tipDialog.setContent(getString(R.string.tip_window_checknet));
tipDialog.show();
}else if(isNetLagException){
tipDialog.setContent(getString(R.string.tip_window_nostable_net));
tipDialog.show();
}else if(isConnException){
tipDialog.setContent(getString(R.string.tip_window_loseconn));
tipDialog.show();
}else{
handlePersubmitOrder(result);
}
} }.execute();

AsyncTask 与 对话框显示 view.WindowManager$BadTokenException: Unable to add window…is not valid; is your a的更多相关文章

  1. Activity has leaked window that was originally added -界面退出时未关闭对话框异常 android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? -

    退出Activity时弹出登录框,点击确定finish当前Activity,结果报了这个错,随后查找资料知道 原因: 是因为退出Activity时没有关闭弹出框,出现了这个错误 解决方法: 只需要在a ...

  2. bug_ _ android.view.WindowManager$BadTokenException: Unable to add window -- token

    ========4       关于android的一个常见错误:Unable to add window --token is not valid android.view.WindowManage ...

  3. Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

    在广播中启动一个Dialog时出现如下错误信息:Caused by: android.view.WindowManager$BadTokenException: Unable to add windo ...

  4. 关于ProgressDialog.show抛出android.view.WindowManager$BadTokenException: Unable to add window

    下午摆弄ProgressDialog,进入就抛错:android.view.WindowManager$BadTokenException: Unable to add window -- token ...

  5. android.view.WindowManager$BadTokenException: Unable to add window

    这是在加载dialog时出现的一个异常.转载地址:http://hi.baidu.com/fbdfp/item/7dea2d0ade9121813d42e23d 扔了好久的android又开始断断续续 ...

  6. android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

      原博客地址:http://aijiawang-126-com.javaeye.com/blog/662336 在Activity中newSpinner是我把mContext传入,但是出了 andr ...

  7. android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an applic

    之前遇到过这样的问题, 04-12 10:40:33.302: E/AndroidRuntime(17213): Caused by: android.view.WindowManager$BadTo ...

  8. Android报错:WindowManager$BadTokenException: Unable to add window -- window has already been added

    很久之前测试通过的代码,现在手机升级了Android7.0后一运行就崩溃,报出这样的错误,具体错误如下: Process: com.example.sho.android_anti_theft, PI ...

  9. WindowManager$BadTokenException: Unable to add window permission denied for this window type

    10-11 11:47:27.472: E/AndroidRuntime(12804): java.lang.RuntimeException: Unable to start activity Co ...

随机推荐

  1. poj 3666 Making the Grade(dp)

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  2. 无Root抓包:Packet Capture 1.2.3

    无Root抓包:Packet Capture 1.2.3 http://www.coolapk.com/apk/app.greyshirts.sslcapture

  3. php之手机号码查归属地

    免费手机号码归属地API查询接口 一.淘宝网API API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=1585078144 ...

  4. C/C++变量命名规则

    变量命名规则是为了增强代码的可读性和easy维护性. 变量命名规则: 一.用最短字符表示最准确的意义. 二.使用变量前缀. 1.  整型前缀 int               nId;        ...

  5. C#构造函数的 "继承" 问题

    首先说明下 之所以用 双引号 是因为构造函数是没有继承的 派生类默认会调用基类的无参数构造函数 比如: public class A         { public A()         { Co ...

  6. (转)vivoxshot 精英版三模转五模模式切换操作方法

    https://yunpan.cn/cPUBWc8vtKpID (提取码:de92) 本帖最后由 韩爱峰 于 2016-3-29 23:00 编辑 碰到不少机友在使用过程中操作不成功,现将我的方法跟大 ...

  7. Microsoft Visual Studio 产品密钥

    Microsoft Visual Studio 2010 产品密钥:YCFHQ-9DWCY-DKV88-T2TMH-G7BHP Microsoft Visual Studio 2013 产品密钥:BW ...

  8. WPF DataGrid 之数据绑定--实例2

    1.前台Grid定义 <!--数据绑定--> <DataGrid Grid.Row="1" Name="gridOne" Margin=&qu ...

  9. 服务器安装VMware ESXI5.5

    一.VMware ESXI5.5 vSphere5.5 VIMSetup下载http://blog.sina.com.cn/s/blog_61c07ac50101gy64.html(1)VMware安 ...

  10. linux make

    linux make file 以下是转载 感谢原作者 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和 ...