简体   繁体   English

在 API 22 上启动 AnimatedVectorDrawable 时出现 NullPointerException

[英]NullPointerException when starting AnimatedVectorDrawable on API 22

I'm using this snippet to start an AnimatedVectorDrawable animation:我正在使用此代码段来启动AnimatedVectorDrawable动画:

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). (基本上我得到了 ImageView 的android:src ,它是一个AnimatedVectorDrawable ,然后我启动了动画)。

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:到目前为止,它在我测试过的每台设备上都运行良好,但是当我在 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)

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.我知道NullPointerException是什么意思,但遗憾的是我认为我无法控制这个。 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).当我尝试在没有target属性的AnimatedVectorDrawable上启动动画时发生崩溃(我使用单个 XML 文件来定义矢量可绘制对象及其动画)。

More precisely, I think android looks into the animated-vector element and throws an error if no target if found.更准确地说,我认为 android 会查看animated-vector元素,如果没有找到target则会抛出错误。 Again, I only encountered this issue on API 22 so far, but I haven't tested other APIs yet...同样,到目前为止我只在 API 22 上遇到过这个问题,但我还没有测试过其他 API ......

I can only think of two solutions for now:我现在只能想到两个解决方案

  • either add at least one target element into the AnimatedVectorDrawable ,要么将至少一个target元素添加到AnimatedVectorDrawable
  • or use a simple 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).或者,如果不需要动画,则使用简单的VectorDrawable (我使用AnimatedVectorDrawable无论我的矢量是否已设置动画,以便以后可以轻松添加动画)。

EDIT编辑

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.我找到了一个简单的解决方法来避免更改所有内容,这也许可以帮助其他人:我只需要向所有不应设置动画的AnimatedVectorDrawable添加一个愚蠢的target

<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.这样AnimatedVectorDrawable没有任何变化,但没有显示错误。 I don't recommend this though, as it's pretty dirty and can lead to other issues.不过我不推荐这样做,因为它很脏,可能会导致其他问题。

Found the same issue on API 21 and 22.在 API 21 和 22 上发现了同样的问题。

The solution that helped me was to define both parameters android:valueFrom and android:valueTo on objectAnimator in xml :帮助我解决的办法是定义这两个参数android:valueFromandroid:valueToobjectAnimator在XML:

<objectAnimator
    android:propertyName="fillColor"
    android:duration="250"
    android:valueFrom="#393749"
    android:valueTo="#00000000"
    android:valueType="colorType"/>

But on API above it's working without android:valueFrom (crazy droid does not save our time:)但是在上面的 API 上,它在没有android:valueFrom的情况下也能工作(疯狂的机器人并不能节省我们的时间:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 启动PreferenceActivity时出现NullPointerException - NullPointerException When Starting PreferenceActivity 通过Intent启动活动时发生NullPointerException - NullPointerException when starting an activity through intent 使用BTrace代理启动应用程序时发生NullPointerException - NullPointerException when Starting an application with BTrace agent 启动新活动时发生 NullPointerException - NullPointerException occurring when starting new activity 应用程序使用Proguard时,Pre Lollipop无法播放AnimatedVectorDrawable - Pre Lollipop cannot play AnimatedVectorDrawable when app use Proguard 在没有NFC的设备上启动活动时,错误:NullPointerException - Error: NullPointerException when starting activity on devices without NFC 播放,Ebeans:启动具有多个DB的应用程序时出现NullPointerException - Play, Ebeans: NullPointerException when starting application with multiple DBs 在启动具有默认值设置的Popupwindow时出现NullPointerException,否则它将起作用 - NullPointerException when starting a Popupwindow with default values set, otherwise it works 在 Java 中停止和启动 Clip 时如何避免 NullPointerException? - How do I avoid a NullPointerException when stopping and starting a Clip in Java? NullPointerException在通知上启动 - NullPointerException on starting on Notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM