Constraint layout Layout Crashing : All Children of constraint layout should have ids to use constraint set

I had the same bug in my code. I had ids for all the views in xml, but I was manually adding a view to the constraint layout(a Tooltip view) with

constraintParent.addView(childView)

and while the dynamically added view is still on the parent if the constraint layout is redrawn (app goes to bg and resumed) this exception was getting triggered.

I fixed it by generating a view id for the dynamic view like this

CustomViewChildView childView = new CustomViewChildView()
childView.setId(View.generateViewId()); 

and then adding it to the constraint layout.


Don't forget to give the ConstraintLayout an id as well.

I forgot this and it crashed my code. This isn't really obvious from the error.


I just figure out how to fix this problem. Notice that is you must declare all view on ConstrainLayout have set ids, check example below:

In case 1 it's working success.

<android.support.constraint.ConstraintLayout 
    android:id="@+id/viewGroup"
    ...>
    <ImageView
        android:id="@+id/imgId"
        ... />
    <TextView
        android:id="@+id/txtId"
        ... />

In case 2 below not working.

<android.support.constraint.ConstraintLayout 
        android:id="@+id/viewGroup"
        ...>
        <ImageView
            android:id="@+id/imgId"
            ... />
        <TextView
          // do not set ids
            ... />

I hope this it will helpful for you.