8

I'm playing with MotionLayout and got a strange issue

When i changed used library version from com.android.support.constraint:constraint-layout:2.0.0-alpha2 to com.android.support.constraint:constraint-layout:2.0.0-alpha3

my app crashed:

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)

Doing some debugging i found some differences in versions of MotionLayout causing this crash:

MotionLayout alpha-3

MotionLayout alpha-2

As you can see in alpha-3 this.mDevModeDraw.draw(canvas, this.mFrameArrayList, this.mScene.getDuration(), this.mDebugPath); will be called and cause crash when trying to get motion scene duration this.mScene.getDuration(), but in alpha-2 it wont be called due to different if condition.

How should I solve this?

Daniele Santi
  • 771
  • 3
  • 24
  • 31
Sergei Klimov
  • 81
  • 1
  • 3
  • To help, we'd need to see how you are using MotionLayout - especially your MotionScene. – jossiwolf Jan 18 '19 at 11:13
  • For me it happened probably when I used merge tag and inflated my own MotionLayout subclass with it. However on alpha2 this doesn't crash but doesn't work either. – Michał Klimczak Feb 06 '19 at 12:23

4 Answers4

3

You haven't posted your XML though you could check if you have the app:layoutDescription attribute on the layout set.

3k-
  • 2,467
  • 2
  • 23
  • 24
2

For me it happened when I subclassed MotionLayout and inflated its contents with <merge... tag. Forgot that the parameters of merge tag are ignored and that you have to manually apply them back in the code.

So it looks like this now:

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)
}

...
Michał Klimczak
  • 12,674
  • 8
  • 66
  • 99
1

Fixed same issue by updating library from

implementation 'androidx.constraintlayout:constraintlayout:2.0.1'

to

implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
Andrew
  • 2,438
  • 1
  • 22
  • 35
0

The underlying reason for this particular crash was a change in AAPT that uncovered a bug in ConstraintLayout's new attributes. This was fixed in one of the betas, so if you're getting into MotionLayout now you should just use the latest stable version. If for some reason you're stuck on an old alpha version you can put these in your values/attrs.xml file to resolve the crash:

    <attr name="duration"/>
    <attr name="flow_horizontalSeparator"/>
    <attr name="flow_verticalSeparator"/>
    <attr name="motionPathRotate"/>
    <attr name="motionProgress"/>
    <attr name="waveDecay"/>

I strongly recommend using the latest stable version 2.0.4 instead.

Jason Pearson
  • 609
  • 4
  • 15