M. Usman Khan
M. Usman Khan

Reputation: 4408

ConstraintSet clone crash

I am trying to clone the ConstraintSet like:

        ConstraintLayout cl = findViewById(R.id.mainActivityParentCl);
        ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(cl);

It crashes on clone(cl),

Caused by: java.lang.RuntimeException: All children of ConstraintLayout must have ids to use ConstraintSet

It doesnt crash when using

implementation 'com.android.support.constraint:constraint-layout:1.0.2'

It crashes on 1.1.3:

implementation 'com.android.support.constraint:constraint-layout:1.1.3'

Solution?

Upvotes: 4

Views: 3226

Answers (1)

Nitin Gurbani
Nitin Gurbani

Reputation: 1240

The problem you're facing is because from com.android.support.constraint:constraint-layout:1.1.0 it is necessary to give id to each view as because when doing the XML part of things via Java/Kotlin code and when especially cloning layout(s) using ConstraintSet which can be further used to create dynamic views, which can be created easily; whose id if not given, gives or throws an exception (at the time of cloning the constraintSet) as yours as because when the ConstraintLayout is redrawn (like in case, when the App goes to background and is then resumed) this exception is triggered (due to lack of the view's accessibility whose id is not given). And as per the Android Developers' guidelines as well you must provide a unique id to each and every view to have proper accessibility to that view.

I hope, this helps you.

Upvotes: 5

Related Questions