Fix a NPE in Keyframe.Factory
@@ -110,8 +110,15 @@ public class Keyframe<T> {
}
public static class Factory {
- private static final SparseArrayCompat<WeakReference<Interpolator>> pathInterpolatorCache =
- new SparseArrayCompat<>();
+ private static SparseArrayCompat<WeakReference<Interpolator>> pathInterpolatorCache;
+
+ // https://github.com/airbnb/lottie-android/issues/464
+ private static SparseArrayCompat<WeakReference<Interpolator>> pathInterpolatorCache() {
+ if (pathInterpolatorCache == null) {
+ pathInterpolatorCache = new SparseArrayCompat<>();
+ }
+ return pathInterpolatorCache;
+ }
private Factory() {
}
@@ -156,7 +163,7 @@ public class Keyframe<T> {
cp2.x = MiscUtils.clamp(cp2.x, -scale, scale);
cp2.y = MiscUtils.clamp(cp2.y, -MAX_CP_VALUE, MAX_CP_VALUE);
int hash = Utils.hashFor(cp1.x, cp1.y, cp2.x, cp2.y);
- WeakReference<Interpolator> interpolatorRef = pathInterpolatorCache.get(hash);
+ WeakReference<Interpolator> interpolatorRef = pathInterpolatorCache().get(hash);
if (interpolatorRef != null) {
interpolator = interpolatorRef.get();
}
@@ -164,7 +171,7 @@ public class Keyframe<T> {
interpolator = PathInterpolatorCompat.create(
cp1.x / scale, cp1.y / scale, cp2.x / scale, cp2.y / scale);
try {
- pathInterpolatorCache.put(hash, new WeakReference<>(interpolator));
+ pathInterpolatorCache().put(hash, new WeakReference<>(interpolator));
} catch (ArrayIndexOutOfBoundsException e) {
// It is not clear why but SparseArrayCompat sometimes fails with this:
// https://github.com/airbnb/lottie-android/issues/452
