I'm using this snippet to start an AnimatedVectorDrawable
animation:
AnimatedVectorDrawable vectorAnim = (AnimatedVectorDrawable) ((ImageView) child).getDrawable();
vectorAnim.start();
(Basically I get the android:src
of the ImageView, which is an AnimatedVectorDrawable
, and then I launch the animation).
It works fine on every device I tested until now, but when I run my app on an API 22 device I get this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.graphics.drawable.AnimatedVectorDrawable.isStarted(AnimatedVectorDrawable.java:427)
at android.graphics.drawable.AnimatedVectorDrawable.start(AnimatedVectorDrawable.java:440)
at com.brickx.creartup.MainRoadmapAdapter.animateViews(MainRoadmapAdapter.java:263)
at com.brickx.creartup.MainRoadmapAdapter.access$100(MainRoadmapAdapter.java:31)
at com.brickx.creartup.MainRoadmapAdapter$1.onInflate(MainRoadmapAdapter.java:66)
at android.view.ViewStub.inflate(ViewStub.java:284)
at com.brickx.creartup.MainRoadmapAdapter.inflateBackground(MainRoadmapAdapter.java:215)
at com.brickx.creartup.MainRoadmapAdapter.onCreateViewHolder(MainRoadmapAdapter.java:71)
at com.brickx.creartup.MainRoadmapAdapter.onCreateViewHolder(MainRoadmapAdapter.java:31)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6519)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5706)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5589)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5585)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2231)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1558)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1518)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:610)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3719)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3436)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3988)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:611)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1197)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:132)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1361)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:874)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1172)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15671)
Any idea on what is going on here?
EDIT
I know what NullPointerException
means, but sadly I think I have no control over this one. And I can't figure out why this exception is being thrown here...
OK, I figured out the root of the problem! Even if I have no idea how to solve this issue properly.
The crash happens when I try to launch the animation on an AnimatedVectorDrawable
which has NO target
attribute (I'm using a single XML file to define the vector drawable and its animations).
More precisely, I think android looks into the animated-vector
element and throws an error if no target
if found. Again, I only encountered this issue on API 22 so far, but I haven't tested other APIs yet...
I can only think of two solutions for now:
target
element into the AnimatedVectorDrawable
,VectorDrawable
if no animation is needed (I used AnimatedVectorDrawable
regardless whether or not my vector was animated, to be able to easily add animations to it later if I wanted to).I found a simple workaround to avoid changing everything, that maybe can help others: I just had to add a dumb target
to all my AnimatedVectorDrawable
that shouldn't be animated.
<target android:name="vector">
<aapt:attr name="android:animation">
<objectAnimator
android:propertyName="alpha"
android:valueTo="1"/>
</aapt:attr>
</target>
That way nothing changes in the AnimatedVectorDrawable
, but the error is not showing. I don't recommend this though, as it's pretty dirty and can lead to other issues.