AnimatorUpdateListener должен вызываться только в потоке пользовательского интерфейса? У меня есть значение анимации, которая обновляет цвет представления. Так выглядит код:
class CustomView extends View {
public void startAnimation() {
ValueAnimator animation = ValueAnimator.ofObject(new ArgbEvaluator(),
mStartColor, mEndColor);
animation.setDuration(500L);
animation = new CustomAnimatorUpdateListener(this);
}
private static class CustomAnimatorUpdateListener implements ValueAnimator.AnimatorUpdateListener {
private final WeakReference<View> mHostViewRef;
CustomAnimatorUpdateListener(@NonNull final View hostView) {
mHostViewRef = new WeakReference<>(hostView);
}
@Override
public void onAnimationUpdate(ValueAnimator animator) {
if (mHostViewRef.get() != null) {
mHostViewRef.get().setBackgroundColor((int) animator.getAnimatedValue()); // Crash on this line
}
}
}
Авария
NullPointerException: Attempt to invoke virtual method 'void android.view.View.setBackgroundColor(int)' on a null object reference
Похоже, что один поток получает ненулевое значение в проверке if mHostViewRef.get ()! = Null, а другой поток выполняет блок if, возвращает ноль, а затем приложение вылетает.
Я хотел бы знать, всегда ли этот AnimatorUpdateListener вызывается в потоке пользовательского интерфейса? или может быть в фоновом потоке?