我在玩MotionLayout时遇到一个奇怪的问题
当我将使用的库版本从com.android.support.constraint:constraint-layout:2.0.0-alpha2
更改为com.android.support.constraint:constraint-layout:2.0.0-alpha3
我的应用程序崩溃了:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: ru.sergik.collapsibleexoplayer, PID: 1376
java.lang.NullPointerException: Attempt to invoke virtual method 'int androidx.constraintlayout.motion.widget.MotionScene.getDuration()' on a null object reference
at androidx.constraintlayout.motion.widget.MotionLayout.dispatchDraw(MotionLayout.java:1634)
at android.view.View.updateDisplayListIfDirty(View.java:19306)
at android.view.View.draw(View.java:20093)
at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
at androidx.constraintlayout.widget.ConstraintLayout.dispatchDraw(ConstraintLayout.java:2072)
at android.view.View.updateDisplayListIfDirty(View.java:19306)
at android.view.View.draw(View.java:20093)
at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
at android.view.View.updateDisplayListIfDirty(View.java:19306)
at android.view.View.draw(View.java:20093)
at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
at android.view.View.updateDisplayListIfDirty(View.java:19306)
at android.view.View.draw(View.java:20093)
at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
at android.view.View.updateDisplayListIfDirty(View.java:19306)
at android.view.View.draw(View.java:20093)
at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
at android.view.View.updateDisplayListIfDirty(View.java:19306)
at android.view.View.draw(View.java:20093)
at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
at android.view.View.draw(View.java:20373)
at com.android.internal.policy.DecorView.draw(DecorView.java:980)
at android.view.View.updateDisplayListIfDirty(View.java:19315)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:686)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:692)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:800)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:3496)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:3283)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2818)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1780)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7827)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
进行一些调试,发现导致该崩溃的MotionLayout版本存在一些差异:
正如您在alpha-3中看到的那样,尝试获取运动场景持续时间this.mDevModeDraw.draw(canvas, this.mFrameArrayList, this.mScene.getDuration(), this.mDebugPath);
时将调用this.mScene.getDuration()
并导致崩溃,但是在alpha-2中,由于if条件不同,因此不会调用它。
我应该如何解决?
答案 0 :(得分:2)
尽管您可以检查布局集上是否具有app:layoutDescription
属性,但是您尚未发布XML。
答案 1 :(得分:1)
对我来说,它发生在我继承MotionLayout
并用<merge...
标签夸大其内容的时候。忘记了合并标记的参数会被忽略,而您必须手动将其重新应用到代码中。
所以现在看起来像这样:
class SomeLayout : MotionLayout {
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
inflate(context, R.layout.some_layout, this)
loadLayoutDescription(R.xml.some_motion_scene)
setTransition(R.id.start, R.id.end)
}
...
答案 2 :(得分:0)
通过更新库修复了同一问题 来自
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
到
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
答案 3 :(得分:0)
这次特定崩溃的根本原因是 AAPT 的一个变化,它发现了 a bug in ConstraintLayout's new attributes。这是在其中一个测试版中修复的,因此如果您现在进入 MotionLayout,您应该只使用最新的稳定版本。如果由于某种原因您停留在旧的 alpha 版本上,您可以将这些放在您的 values/attrs.xml
文件中以解决崩溃:
<attr name="duration"/>
<attr name="flow_horizontalSeparator"/>
<attr name="flow_verticalSeparator"/>
<attr name="motionPathRotate"/>
<attr name="motionProgress"/>
<attr name="waveDecay"/>
我强烈建议改用最新的稳定版本 2.0.4。