Android中无法解析ImageButton的id。

3

我有一个滚动视图,里面有4个ImageButtons。但是,当我在约束布局中定义约束时,XML文件会出现错误消息:"无法解析符号@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,正如您在图中所看到的那样。t 我会感激每一个评论。

1
你尝试过清理并重新构建你的项目吗? - karan
2个回答

5

看起来很像一个Android Studio的问题。你尝试过以下任何一种方法吗:

  • 重新同步gradle文件与项目
  • 清理并重新构建
  • 无效化缓存并重新启动?

谢谢,Tanya。这很有帮助。 - VanessaF

0

看起来你忘记在 id 的开头加上+符号了。

只需用以下代码替换出错的行:

app:layout_constraintEnd_toStartOf="@+id/imageButton_2"

感谢Bugra的回答。不幸的是,这并没有改变任何事情,错误仍然存在。 - VanessaF

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接