[Android] NoSuchMethodError - removeOnGlobalLayoutListener
07-15 15:19:04.680: E/AndroidRuntime(9733): java.lang.NoSuchMethodError: android.view.ViewTreeObserver.removeOnGlobalLayoutListener
There are two methods in ViewTreeObserver
with almost the same name.
removeOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener victim)
(on then global) is a method that was added in API 16. It replaces
removeGlobalOnLayoutListener(ViewTreeObserver.OnGlobalLayoutListener victim)
(global then on) which has existed since API 1, but which is now deprecated. Compatibility libraries can cause confusion about these two methods; it can appear present at compile-time but fail subsequently.
This code thwarts the error:
try {
thing.removeOnGlobalLayoutListener(victim);
} catch (NoSuchMethodError x) {
thing.removeGlobalOnLayoutListener(victim);
}
So does this code:
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
thing.removeGlobalOnLayoutListener(victim);
} else {
thing.removeOnGlobalLayoutListener(victim);
}
android - Why does removeOnGlobalLayoutListener throw a NoSuchMethodError_ - Stack Overflow.pdf