问题描述
我有一些code。使用该编译成功 ViewTreeObserver#removeOnGlobalLayoutListener(...)
,并在运行时,此方法将引发的NoSuchMethodError
。为什么呢?
I have some code that compiles successfully using ViewTreeObserver#removeOnGlobalLayoutListener(...)
and when it runs, this method throws NoSuchMethodError
. Why?
推荐答案
有的两个的方法 ViewTreeObserver
几乎相同的名称。
There are two methods in ViewTreeObserver
with almost the same name.
<$c$c>removeOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener受害者)
(在和全球)是添加在API的方法16.替换
(on then global) is a method that was added in API 16. It replaces
<$c$c>removeGlobalOnLayoutListener(ViewTreeObserver.OnGlobalLayoutListener受害者)
(全球和的),这是自1 API已经存在,但现在是很precated。
(global then on) which has existed since API 1, but which is now deprecated.
这两种方法都可以出现在编译时present(如果您正在构建对杰利贝恩或更高),但较新的人会失败在pre-杰利贝恩设备。
Both methods can appear present at compile-time (if you're building against Jellybean or higher) but the newer one will fail on pre-Jellybean devices.
这code阻挠错误:
try {
thing.removeOnGlobalLayoutListener(victim);
} catch (NoSuchMethodError x) {
thing.removeGlobalOnLayoutListener(victim);
}
那么,这是否code:
So does this code:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
thing.removeGlobalOnLayoutListener(victim);
} else {
thing.removeOnGlobalLayoutListener(victim);
}
这篇关于为什么removeOnGlobalLayoutListener抛出的NoSuchMethodError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!