来自:《Error : BinderProxy@45d459c0 is not valid; is your activity running?》,《Anroid 异常:is not valid; is your activity running?》
在显示对话框的时候遇到这个问题: BinderProxy@45d459c0 is not valid; is your activity running?
以下是两篇参考的帖子:
一、
This is most likely happening because you are trying to show a dialog after execution of a background thread, while the Activity is being destroyed.
I was seeing this error reported once in a while from some of my apps when the activity calling the dialog was finishing for some reason or another when it tried to show a dialog. Here's what solved it for me:
if(!((Activity) context).isFinishing())
{
//show dialog
}
二、
是由于有activity时依附于另一个activity的,当被依附的activity产生错误的时候,该activity没有了靠山而产生错误(或者是调用了一个已经被finish()的activity)。一般在创建alertdialog.builder是很容易见到。
对于这个错误还没有根本的解决办法,唯一的办法就是改变该activity依附于另一activity的事实,将其放入另一activity。
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对话框的时候,参数context 指定成了this,即指向当前子Activity的context。但子Activity是动态创建的,不能保证一直存在。其父Activity的context是稳定存在的,所以有下面的解决办法。
解决办法:
将context替换为getParent()即可。
【个人总结】显示对话框的两个事项:
1、显示前判断activity是否isFinishing;
2、不要保存静态的context