In my dev console, I get the following error:
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@405126b8 is not valid; is your activity running?
It's the follow line: alertDialog = new AlertDialog.Builder(Main.this).create();
Here is my code:
@Override
publ开发者_如何学运维ic void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);
alertDialog = new AlertDialog.Builder(Main.this).create();
LoadData();
}
I dont no whats wrong.
You are trying to use a bad context here, try to use the right context. See this,
Bad token Exception
I don't know if an activity is already considered to be "running" in onCreate. Have you tried the same code in onResume()?
Better yet, overwrite onCreateDialog() and later showDialog(). (see http://developer.android.com/guide/topics/ui/dialogs.html).
Finally, you don't seem to set any properties on the Builder before creating it - no title, no message - maybe one of those is required?
I was seeing this error reported once in a while from some of my apps, and here's what solved it for me:
if(!((Activity) context).isFinishing())
{
//show dialog
}
All the other answers out there seem to be doing weird things like iterating through the list of running activities, but this is much simpler and seems to do the trick.
精彩评论