ricardopereira
ricardopereira

Reputation: 11683

ConstraintSet.createHorizontalChain IllegalArgumentException: must have 2 or more widgets in a chain

I'm facing an error while setting a horizontal chain constrain using ConstraintLayout:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: me.optimize.debug, PID: 18532
    java.lang.RuntimeException: Unable to start activity ComponentInfo{me.optimize.debug/me.optimize.post.PostActivity}: java.lang.IllegalArgumentException: must have 2 or more widgets in a chain
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
     Caused by: java.lang.IllegalArgumentException: must have 2 or more widgets in a chain
        at android.support.constraint.ConstraintSet.createHorizontalChain(ConstraintSet.java:1032)
        at android.support.constraint.ConstraintSet.createHorizontalChain(ConstraintSet.java:1005)

I'm basically adding the constraint like:

set.createHorizontalChain(buttonA.id, ConstraintSet.LEFT, buttonC.id, ConstraintSet.RIGHT, arrayOf(buttonA.id, buttonB.id, buttonC.id).toIntArray(), arrayOf(0f, 0f).toFloatArray(), 0)

I have more than 2 widgets because I'm passing buttonA, buttonB and buttonC.

Upvotes: 1

Views: 584

Answers (1)

ricardopereira
ricardopereira

Reputation: 11683

Well, while writing my question I decided to look into the android.support.constraint.ConstraintSet.createHorizontalChain code that I found on GitHub and guess what:

createHorizontalChain

The array of chainIds and weights need to be the same!

Upvotes: 1

Related Questions