我正在使用以下代码片段启动AnimatedVectorDrawable
动画:
AnimatedVectorDrawable vectorAnim = (AnimatedVectorDrawable) ((ImageView) child).getDrawable();
vectorAnim.start();
(基本上,我得到ImageView的
android:src
,它是一个AnimatedVectorDrawable
,然后启动动画)。到目前为止,它在我测试过的每台设备上都能正常运行,但是当我在API 22设备上运行我的应用程序时,出现此错误:
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)
对这里发生的事情有任何想法吗?
编辑
我知道
NullPointerException
的含义,但可悲的是,我认为我对此无能为力。而且我不知道为什么在这里抛出此异常... 最佳答案
好的,我找出问题的根源!即使我不知道如何正确解决此问题。
当我尝试在不具有AnimatedVectorDrawable
属性的target
上启动动画时发生崩溃(我正在使用single XML file定义矢量可绘制对象及其动画)。
更准确地说,我认为android会查看animated-vector
元素,如果找不到target
则会引发错误。同样,到目前为止,我仅在API 22上遇到过此问题,但尚未测试其他API ...
我现在只能想到两种解决方案:
将至少一个target
元素添加到AnimatedVectorDrawable
中,
或者如果不需要动画,则使用简单的VectorDrawable
(我使用了AnimatedVectorDrawable
不管我的矢量是否已动画,以便以后可以在其中轻松添加动画)。
编辑
我找到了一个简单的解决方法来避免更改所有内容,这可能会对其他人有所帮助:我只需要在所有target
上添加一个不应设置动画的哑巴AnimatedVectorDrawable
。
<target android:name="vector">
<aapt:attr name="android:animation">
<objectAnimator
android:propertyName="alpha"
android:valueTo="1"/>
</aapt:attr>
</target>
这样,
AnimatedVectorDrawable
中没有任何变化,但是错误没有显示。但我不建议这样做,因为它很脏,并且可能导致其他问题。