ConstraintSet.createHorizo​​ntalChain IllegalArgumentException:链中必须有2个或更多小部件

时间:2019-04-11 10:37:58

标签: android kotlin android-constraintlayout

使用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)

我基本上要添加约束,例如:

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

我有两个以上的小部件,因为我传递了buttonAbuttonBbuttonC

1 个答案:

答案 0 :(得分:0)

好吧,在写我的问题时,我决定研究一下在GitHub上找到的android.support.constraint.ConstraintSet.createHorizontalChain代码,然后猜测一下:

createHorizontalChain

chainIdsweights的数组必须相同!