android - Lottie Animation setAnimation() 结果 IllegalStateException

标签 android lottie

我今天想检查 iOS 和 Android 的 Lottie lib,我在 after effect 中制作了一个简单的指示器动画并将其导出到 .json 文件,它只适用于 iOS。当我在 Android 上测试相同的文件时,出现以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.lottietest/com.test.lottietest.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.airbnb.lottie.LottieAnimationView.setAnimation(java.lang.String)' on a null object reference
这是我的 Activity XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mtids.lottietest.MainActivity">

    <view
        class="com.airbnb.lottie.LottieAnimationView"
        id="@+id/animationView"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="133dp"
        android:layout_marginTop="20dp"
        android:onClick="animate"
        android:text="animate" />
    
</RelativeLayout>
Activity 代码
public class MainActivity extends AppCompatActivity {
    LottieAnimationView anim;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        anim = (LottieAnimationView) findViewById(R.id.animationView);
        anim.setAnimation("indicator.json");
    }

    public void animate(View view) {
        anim.loop(true);
        anim.playAnimation();
    }
}

最佳答案

i'v changed the id property to android : id="@+id/animationView" instead of id="@+id/animationView" only , and the error message become java.lang.IllegalStateException: Unable to find file indicator.json



正如您正确注意到的,首先您应该更改 idandroid:id .

其次,来自sources of setAnimation(String) :

Sets the animation from a file in the assets directory.



显然,assets 中没有名为“indicator.json”的文件。目录。

关于android - Lottie Animation setAnimation() 结果 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43142429/

相关文章:

android - 圆形选项卡布局

android - 如何在android中动态创建 View 并添加到主视图上?

android - 无法从 URL 加载 Lottie 动画

android - Lottie 动画库 Z 排序问题

react-native - 类型错误 : undefined is not an object (evaluating 'subscription[nativeEmitterSubscriptionKey]' ) when using animated spinner

android - CMake交叉编译包 : how to set linker path to find compiler support library?

android - fragment 生命周期 : when "ondestroy" and "ondestroyview" are not called?

android - 在 Android 中执行添加 2 个 LARGE float 数组的快速方法。可选的 JNI 或 Opengl ES

android - 如何在运行时更新 LottieCompose ClipSpec

animation - Flutter 上的 Lottie 闪屏