无法在 Android 中解析 ImageButton 的 ID

Cannot resolve id of ImageButton in Android

我有一个包含 4 个 ImageButton 的滚动视图。但是,当我在约束布局中定义约束时,我在 XML 文件中收到错误消息:"Cannot resolve symbol @id/imageButton "。我不明白这个错误消息,因为我已经定义了图像按钮的 ID。任何人都可以帮我吗?这是部分代码:

 <ScrollView
        app:layout_constraintTop_toBottomOf="@id/toolbar_mainActivity"
        app:layout_constraintBottom_toTopOf="@id/bottom_layout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_width="0dp"
        android:layout_height="0dp">
        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:context=".MainActivity"
            tools:ignore="ExtraText">


            <ImageButton
                android:id="@+id/imageButton_1"
                android:layout_width="0dp"
                android:layout_height="128dp"
                android:layout_marginTop="12dp"
                android:background="#00000000"
                android:scaleType="fitCenter"
                app:layout_constraintEnd_toStartOf="@id/imageButton_2"
                app:layout_constraintHorizontal_chainStyle="spread"
                app:layout_constraintHorizontal_weight="1"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/test_dish_1" />

            <ImageButton
                android:id="@+id/imageButton_2"
                android:layout_width="0dp"
                android:layout_height="128dp"
                android:layout_marginTop="12dp"
                android:background="#00000000"
                android:scaleType="fitCenter"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_weight="1"
                app:layout_constraintStart_toEndOf="@id/imageButton_1"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/test_dish_1" />

            <ImageButton
                android:id="@+id/imageButton_4"
                android:layout_width="0dp"
                android:layout_height="128dp"
                android:layout_marginTop="12dp"
                android:background="#00000000"
                android:scaleType="fitCenter"
                app:layout_constraintEnd_toStartOf="@id/imageButton_3"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/imageButton_1"
                app:layout_constraintVertical_chainStyle="packed"
                app:srcCompat="@drawable/test_dish_1" />

            <ImageButton
                android:id="@+id/imageButton_3"
                android:layout_width="0dp"
                android:layout_height="128dp"
                android:layout_marginTop="12dp"
                android:background="#00000000"
                android:scaleType="fitCenter"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/imageButton_4"
                app:layout_constraintTop_toBottomOf="@id/imageButton_1"
                app:srcCompat="@drawable/test_dish_1" />






        </android.support.constraint.ConstraintLayout>
    </ScrollView>

错误显示在例如以下行中: 'app:layout_constraintEnd_toStartOf="@id/imageButton_2"'

这是 Android Studio 中 xml 文件中错误消息的屏幕截图。对我来说最引人注目的是我已经为imageButton_2定义了id,如图所示。

我会很感激每一条评论。

你好像忘了在id开头加上+符号

只需将出错的行替换为:

app:layout_constraintEnd_toStartOf="@+id/imageButton_2"

看起来很像 Android Studio 问题。你试过这些吗:

  • 将项目与 gradle 个文件重新同步
  • 清理并重建
  • 使cahce失效并重启?