Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MotionLayout crashes when migrating from alpha-2 to alpha-3

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?

like image 433
Sergei Klimov Avatar asked Jan 16 '19 09:01

Sergei Klimov


People also ask

What is MotionLayout Android?

MotionLayout is a layout type that helps you manage motion and widget animation in your app. MotionLayout is a subclass of ConstraintLayout and builds upon its rich layout capabilities. As part of the ConstraintLayout library, MotionLayout is available as a support library and is backwards-compatible to API level 14.

What is Androidx ConstraintLayout?

GitHub - androidx/constraintlayout: ConstraintLayout is an Android layout component which allows you to position and size widgets in a flexible way.

Which is at least required for ConstraintLayout?

To define a view's position in ConstraintLayout , you must add at least one horizontal and one vertical constraint for the view.


3 Answers

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

like image 73
3k- Avatar answered Nov 15 '22 08:11

3k-


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

...
like image 42
Michał Klimczak Avatar answered Nov 15 '22 06:11

Michał Klimczak


Fixed same issue by updating library from

implementation 'androidx.constraintlayout:constraintlayout:2.0.1'

to

implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
like image 43
Andrew Avatar answered Nov 15 '22 07:11

Andrew