Reputation: 2362
In my App I have a threat which loads some images from a server. After the download of the images they are shown in a dialog. This is working, but if the user leaves the screen via the back button, my app is crashing with this logcat output:
09-21 09:54:14.553: ERROR/AndroidRuntime(486): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@4053fb28 is not valid; is your activity running?
The App is running until the old activity calls the code below (creating a custom dialog).
I have figured out there it is crashing: I am calling a Handler after the images are downloaded. In the handler I do following:
final Dialog dialog = new Dialog(Product.this);
dialog.setContentView(R.layout.imageview_inflator);
dialog.setTitle("Choose a Picture");
dialog.setCancelable(true);
// set up image view
final ImageView img = (ImageView) dialog.findViewById(R.id.imageView);
img.setImageBitmap(ImageLoader.cache.get(pic_url[pictureCounter]));
dialog.show();
How can I avoid the crashing. I think I should somehow avoid showing the dialog if the activity is not the one running.
Upvotes: 1
Views: 599
Reputation: 24021
You are getting this error bcoz when you leave the activity, your thread is still running and it is calling your handler to show dialog and the dialog has no activity over which it can be shown.
You can avoid it by putting your dialog code inside try-catch
as it will stop your app from crashing.
Upvotes: 1