为什么以前版本在后台线程操作UI时不抛出异常? [英] Why is no exception thrown when operating UI in a background thread in previous versions?

查看:39
本文介绍了为什么以前版本在后台线程操作UI时不抛出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几年前我们发布了一个android APP,在一个子线程中使用了一个View.setVisibility()方法.这几年一直没问题,直到Android O Developer Preview 4.当我在 Android O Developer Preview 4 上测试我们的 APP 时,我发现子线程中的 View.setVisibility() 会抛出如下异常:

We released an android APP several years ago, and there's a View.setVisibility() method used in a sub thread. It has been no problem in these years until Android O Developer Preview 4. When I test our APP on Android O Developer Preview 4, I found View.setVisibility() in a sub thread will throw a exception as follows:

android.view.ViewRootImpl$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图.

我已将此方法移至主线程并解决了此问题.

I've moved this method to main thread and fixed this problem.

但我的问题是:

  1. 为什么以前的版本没有抛出异常(甚至在 Android O Developer Preview 1 中也没有异常)?

  1. Why is no exception thrown in previous versions(even no exception in Android O Developer Preview 1)?

Android O 的哪些变化导致了这个问题?我们应该向我们的客户解释,但我们在 Android 开发者的网站上找不到证据.

What change in Android O leads to this problem? We should explain it to our customer but we can't find the proof on Android developer's website.

推荐答案

这个异常 在以前的版本中被抛出.它从未被可靠地抛出.无论如何,从后台线程访问 UI 元素总是错误的.您的代码不是线程安全的,并且它可能总是表现出未报告的细微错误.您应该修复您的代码.

This exception was thrown in previous versions. It's just never been thrown reliably. Regardless, it's always been wrong to access UI elements from a background thread. Your code is not thread safe, and it's possible that it has always exhibited subtle bugs that have gone unreported. You should fix your code.

这篇关于为什么以前版本在后台线程操作UI时不抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆