我正在使用此代码段来启动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
意味着什么,但遗憾的是我认为我无法控制这一点。我无法弄清楚为什么会抛出这个异常......
答案 0 :(得分:3)
好的,我想出了问题的根源!即使我不知道如何正确解决这个问题。
当我尝试在没有AnimatedVectorDrawable
属性的target
上启动动画时发生崩溃(我使用single XML file来定义矢量drawable及其动画)。
更确切地说,我认为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
没有任何变化,但错误没有显示。我不推荐这个,因为它非常脏,可能导致其他问题。
答案 1 :(得分:0)
在API 21和22中发现了相同的问题。
对我有帮助的解决方案是在xml中的android:valueFrom
上同时定义参数android:valueTo
和objectAnimator
<objectAnimator
android:propertyName="fillColor"
android:duration="250"
android:valueFrom="#393749"
android:valueTo="#00000000"
android:valueType="colorType"/>
但是在上面的API上,它没有android:valueFrom
也可以工作(疯狂的机器人无法节省我们的时间:)