android BadTokenException之token is not valid

Android利用ActivityGroup加载子Activity,子Activity调用对话框,弹出:

04-14 19:07:53.947: E/AndroidRuntime(857): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@405a3cd0 is not valid; is your activity running?

ERROR/AndroidRuntime(10104): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@45a58ee0 is not valid; is your activity running?

原因分析:

 

因为new对话框的时候,参数content 指定成了this,即指向当前子Activity的content。但子Activity是动态创建的,不能保证一直存在。其父Activity的content是稳定存在的,所以有下面的解决办法。

解决办法:

将content替换为getParent()即可。 如下

this.mPD = ProgressDialog.show(((Activity)this.mContext).getParent(), null, this.mContext.getString(R.string.login_wait_tip), true, false);


你可能感兴趣的:(exception)