Lottie Animation setAnimation() results IllegalStateException
Asked Answered
R

4

7

I want to check Lottie lib for both iOS and Android today, I made a simple indicator animation in after effect and export it to .json file and it just works on iOS. When I test the same file on Android I got the following error:

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

here is my 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 code

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();
    }
}
Rosinarosinante answered 31/3, 2017 at 13:51 Comment(9)
debug this line anim = (LottieAnimationView) findViewById(R.id.animationView); it returns null may be library synch issue or view not declared correctly in xml.Brickkiln
I believe it is View, not viewSurbase
well i think the issue is with indicator.json file path, if any use this lib before, plz could you mention where exactly to put the .josn file ??Rosinarosinante
@TimCastelijns, it's <view>.Hypophyge
@AliAdil, are you sure you have successfully imported Lottie dependency?Hypophyge
@Hypophyge yes, my friendRosinarosinante
It should not be an issue, but just try <com.airbnb.lottie.LottieAnimationView /> instead of <view class="com.airbnb.lottie.LottieAnimationView>. Also try to navigate to LottieAnimationView.Hypophyge
I already try it and got the same error !, i will try to use version 1.5.3 instead of 2.0.0Rosinarosinante
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.jsonRosinarosinante
H
10

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

As you correctly noticed, first you should change id to android:id.

Secondly, from the sources of setAnimation(String):

Sets the animation from a file in the assets directory.

Obviously, you have no file named "indicator.json" in assets directory.

Hypophyge answered 31/3, 2017 at 15:8 Comment(1)
Well, as i said before the issue was in the .json file it should be under app/src/main/assets/ now its working ... thank u @HypophygeRosinarosinante
U
6

You have to make a raw resource directory and then paste the "indicator.json" file there.

Change :

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();
    }
}

To:

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(R.raw.indicator);
    }

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

And in the xml file :

Change:

<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" />

To:

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

These changes worked for me.

Undershoot answered 11/7, 2020 at 14:30 Comment(0)
T
2

Simply add this line to programatically change the animation.

// i have used data binding

binding.animationView.setAnimation(R.raw.paymentsuccess);
Tricia answered 31/3, 2021 at 7:26 Comment(0)
F
0

Update version to:

implementation 'com.airbnb.android:lottie:3.0.3'
Forewing answered 10/6, 2020 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.