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();
}
}
anim = (LottieAnimationView) findViewById(R.id.animationView);
it returns null may be library synch issue or view not declared correctly in xml. – BrickkilnView
, notview
– Surbase<com.airbnb.lottie.LottieAnimationView />
instead of<view class="com.airbnb.lottie.LottieAnimationView>
. Also try to navigate toLottieAnimationView
. – Hypophyge