在为 Facebook 关键帧动画反序列化输入流时尝试获取空数组的长度

Attempt to get length of null array when deserializing inputstream for Facebook Keyframes animation

我正在尝试使用 Facebook 关键帧动画框架。我尝试了他们的示例并且它工作正常,问题是当我尝试使用另一个 JSON 来制作另一个动画时,我在反序列化输入流时得到一个空指针。我会附上我的代码和错误

    InputStream stream = getResources().openRawResource(R.raw.animation);
    KFImage kfImage = null;
    try {
        kfImage = KFImageDeserializer.deserialize(stream);
    } catch (IOException e) {
        e.printStackTrace();
    }
    KeyframesDrawable kfDrawable = new KeyframesDrawableBuilder().withImage(kfImage).build();
    
    mAnimation.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    mAnimation.setImageDrawable(kfDrawable);
    mAnimation.setImageAlpha(0);
    kfDrawable.startAnimation();

错误堆栈跟踪:

    java.lang.NullPointerException: Attempt to get length of null array
            at com.facebook.keyframes.util.ArgCheckUtil.checkTimingCurveObjectValidity(ArgCheckUtil.java:53)
            at com.facebook.keyframes.model.KFFeature.<init>(KFFeature.java:214)
            at com.facebook.keyframes.model.KFFeature$Builder.build(KFFeature.java:170)
            at com.facebook.keyframes.deserializers.KFFeatureDeserializer.readObject(KFFeatureDeserializer.java:90)
            at com.facebook.keyframes.deserializers.KFFeatureDeserializer.readObjectImpl(KFFeatureDeserializer.java:32)
            at com.facebook.keyframes.deserializers.KFFeatureDeserializer.readObjectImpl(KFFeatureDeserializer.java:29)
            at com.facebook.keyframes.deserializers.AbstractListDeserializer.readList(AbstractListDeserializer.java:35)
            at com.facebook.keyframes.deserializers.KFImageDeserializer.readObject(KFImageDeserializer.java:57)
            at com.facebook.keyframes.deserializers.KFImageDeserializer.deserialize(KFImageDeserializer.java:40)
            at MyFragment.onCreateView(MyFragment.java:83)
            at android.app.Fragment.performCreateView(Fragment.java:2053)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
            at android.app.BackStackRecord.run(BackStackRecord.java:834)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
            at android.app.FragmentManagerImpl.run(FragmentManager.java:447)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5597)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:984)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

加载动画中 "feature_animations" 数组中的每个条目必须包含 "timing_curves" 属性。它的大小必须等于关键帧的数量 - 1。 关键帧值存储在通常位于 "timing_curves".

正上方的 "key_values" 属性 中

此字段必须始终存在。可以为空但不能为null。

Timing Curves describe the pace with which a transform changes between each keyframe. Each timing curve is modeled as a cubic bezier curve from point [0, 0] to [1, 1], where the X value is the progression in time from the origin keyframe to the destination keyframe, and the Y value describes the amount of change in the value at a given time: origValue + (destValue - origValue) * Y.

Source of quote

ArgCheckUtil 验证动画文件失败的下一个条件:

/**
   * Checks that the format of a timing curve 3D float array is valid.  The number of timing curves
   * for an animation should be equal to the number of keyframes - 1.
   * @param timingCurves the 3D float array to check
   * @param keyFrameQuantity the number of key frames this animation has
   * @return true if the format is valid, false otherwise
   */
  public static boolean checkTimingCurveObjectValidity(...

Source code link on GitHub

你也可以在

android-sample项目中分析sample animation file