对话框使我的应用程序崩溃
在我的应用程序中,我遇到了从服务器加载一些图像的威胁。 下载图像后,它们将显示在对话框中。 这是可行的,但如果用户通过后退按钮离开屏幕,我的应用程序将崩溃并显示以下 logcat 输出:
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?
应用程序正在运行,直到旧活动调用下面的代码(创建自定义对话框)。
我发现它崩溃了: 下载图像后我正在调用处理程序。在处理程序中,我执行以下操作:
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();
如何避免崩溃。我认为如果活动不是正在运行的活动,我应该以某种方式避免显示对话框。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您离开活动时,您会收到此错误,因为您的线程仍在运行,并且它正在调用处理程序来显示对话框,并且该对话框没有可以显示的活动。
您可以通过将对话框代码放入
try-catch
中来避免这种情况,因为它会阻止您的应用程序崩溃。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.