约束布局布局崩溃:约束布局的所有子项都应具有 id 才能使用约束集

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

更新到com.android.support.constraint:constraint-layout:1.1.0

约束布局崩溃说:

All children of constraint layout should have ids to use constraint set

我已经为所有视图设置了 ID,即使它崩溃了。

java.lang.RuntimeException: All children of ConstraintLayout must have ids to use ConstraintSet at android.support.constraint.ConstraintSet.clone(ConstraintSet.java:687) at com.zoho.notebook.views.SettingsViewNavBar.showNoteSettingsView(SettingsViewNavBar.java:594) at com.zoho.notebook.views.SettingsViewNavBar.onClick(SettingsViewNavBar.java:303)

我的代码中有同样的错误。我有 xml 中所有视图的 ID,但我使用

手动将视图添加到约束布局(工具提示视图)
constraintParent.addView(childView)

并且如果重新绘制约束布局(应用程序转到 bg 并恢复),动态添加的视图仍在父级上,则会触发此异常。

我通过像这样为动态视图生成视图 ID 来修复它

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

然后将其添加到约束布局中。

我刚刚弄清楚如何解决这个问题。请注意,您必须声明 ConstrainLayout 上的所有视图都已设置 ID,请检查以下示例:

情况 1 成功。

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

如果下面的情况 2 不工作。

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

希望对您有所帮助。

不要忘记也给 ConstraintLayout 一个 id

我忘了这个,它使我的代码崩溃了。这从错误中并不是很明显。

在我意识到我没有为 ConstraintLayout 中的某些子元素提供 ID 之前,我一直收到此错误。所以,请确保您也不要忘记这样做。

以我为例 不要忘记将 ID 添加到 constraintLayout

的所有子项

我尝试在 onGlobalLayout() 添加新视图时出现此错误(在主布局完成后)。布局中的所有 ID 都是正确的,设置了新的动态视图 ID(setId())。但是故障"All Children of constraint layout should have ids to use constraint set"一直在发生。

当我将 View 添加到 onCreate() 时,一切都很顺利。我只需要改变逻辑,但是关于如何放置新的 View,因为其他视图的位置未在 onCreate().

确定