[Android] Duplicate id @+id/xxx, already defined earlier in this layout

本文介绍了解决Android开发中重复ID问题的两种方法:一种是在代码中避免使用相同ID,另一种是通过设置LintError错误检查来提醒开发者。详细步骤包括从Preferences进入Android选项,搜索DuplicateIds并调整严重级别为Warning。
這個訊息是說,你重複使用相同的id,為了避免混淆,要你使用不同的id。

其實這個訊息也是可以解掉的,當然最簡單的解法就是你不要使用相同的id就好了。不過萬一你是幫別人改code的時候跳出來呢?那也有解法可以解。


  1. 首先從Window點入,選擇list最下面的Preferences
  2. 選擇Android,搜尋DuplicateIds,你可以在上方的text column中輸入du...去篩選
  3. 在Severity下方的彈出式選單中選擇Warning(原始應該是fatal)
  4. 點選ok,讓系統自己跑一次,就完成了
選擇最下面的Preferences

依序進行點選

這個方法是把Lint Error錯誤檢查的某一項檢查的錯誤改成警告,也可以參考這篇 文章

Duplicate id `@+id/noupdatetext` originally defined here <!-- 保留 layout 标签但不声明 data --> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/update" android:layout_width="720dp" android:layout_height="386dp" android:layout_marginLeft="600dp" android:layout_marginTop="291dp" android:background="#FFFFFF"> <!--提示--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/tishi" android:layout_width="600dp" android:layout_height="56dp" android:layout_marginLeft="60dp" android:layout_marginTop="40dp" android:background="#FFFFFF" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--提示框--> <TextView android:id="@+id/tishikuang" android:layout_width="600dp" android:layout_height="56dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="#000000" android:textSize="36sp" android:text="@string/update_tips_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#FFFFFF" app:layout_constraintStart_toStartOf="@+id/tishi" app:layout_constraintTop_toTopOf="@+id/tishi" /> </androidx.constraintlayout.widget.ConstraintLayout> <!--有数据可更新--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/youshujukegengxin" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="60dp" android:layout_marginTop="128dp" android:background="#FFFFFF" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--有数据可提示文言--> <TextView android:id="@+id/youshujukegengxintext" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="#99000000" android:textSize="28sp" android:text="@string/update_updatable_data_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#FFFFFF" app:layout_constraintStart_toStartOf="@+id/youshujukegengxin" app:layout_constraintTop_toTopOf="@+id/youshujukegengxin" /> </androidx.constraintlayout.widget.ConstraintLayout> <!--查看--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/chakan" android:layout_width="280dp" android:layout_height="64dp" android:layout_marginLeft="60dp" android:layout_marginTop="272dp" android:background="#FFFF00" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--有数据可提示文言--> <TextView android:id="@+id/chakantext" android:layout_width="280dp" android:layout_height="64dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="#FFFFFF" android:textSize="28sp" android:text="@string/update_watch_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#B87333" app:layout_constraintStart_toStartOf="@+id/chakan" app:layout_constraintTop_toTopOf="@+id/chakan" /> </androidx.constraintlayout.widget.ConstraintLayout> <!--取消--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/quxiao" android:layout_width="280dp" android:layout_height="64dp" android:layout_marginLeft="380dp" android:layout_marginTop="272dp" android:background="#FFFF00" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--有数据可提示文言--> <TextView android:id="@+id/quxiaotext" android:layout_width="280dp" android:layout_height="64dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="#FFFFFF" android:textSize="28sp" android:text="@string/update_cancel_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#818181" app:layout_constraintStart_toStartOf="@+id/quxiao" app:layout_constraintTop_toTopOf="@+id/quxiao" /> </androidx.constraintlayout.widget.ConstraintLayout> <!--没有数据可更新--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/noupdate" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="60dp" android:layout_marginTop="128dp" android:background="#FFFFFF" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--没有数据可提示文言--> <TextView android:id="@+id/noupdatetext" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="#99000000" android:textSize="28sp" android:text="@string/update_no_updatable_data_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#FFFFFF" app:layout_constraintStart_toStartOf="@+id/noupdate" app:layout_constraintTop_toTopOf="@+id/noupdate" /> </androidx.constraintlayout.widget.ConstraintLayout> <!--我知道了--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/iknow" android:layout_width="280dp" android:layout_height="64dp" android:layout_marginLeft="220dp" android:layout_marginTop="272dp" android:background="#FFFF00" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--我知道了文言--> <TextView android:id="@+id/iknowtext" android:layout_width="280dp" android:layout_height="64dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="#FFFFFF" android:textSize="28sp" android:text="@string/update_konwed_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#818181" app:layout_constraintStart_toStartOf="@+id/iknow" app:layout_constraintTop_toTopOf="@+id/iknow" /> </androidx.constraintlayout.widget.ConstraintLayout> <!--存储空间不足--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/nospace" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="60dp" android:layout_marginTop="128dp" android:background="#FFFFFF" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--有数据可提示文言--> <TextView android:id="@+id/nospacetext" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="#99000000" android:textSize="28sp" android:text="@string/update_no_space_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#FFFFFF" app:layout_constraintStart_toStartOf="@+id/nospace" app:layout_constraintTop_toTopOf="@+id/nospace" /> </androidx.constraintlayout.widget.ConstraintLayout> <!--是否结束数据更新--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/cancel" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="60dp" android:layout_marginTop="128dp" android:background="#FFFFFF" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--有数据可提示文言--> <TextView android:id="@+id/canceltext" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="#99000000" android:textSize="28sp" android:text="@string/update_end_update_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#FFFFFF" app:layout_constraintStart_toStartOf="@+id/cancel" app:layout_constraintTop_toTopOf="@+id/cancel" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout> </layout> 报错的是什么意思??
最新发布
09-09
你遇到的报错信息: ``` Duplicate id `@+id/noupdatetext` originally defined here ``` ### ✅ 报错含义解释: 这是 **Android 编译时资源冲突错误**,意思是: > **你在同一个布局文件中重复使用了相同的 `id` 名称 `noupdatetext`**,这在 Android 中是不允许的。 Android 要求每个 `id` 在同一个布局文件中是**唯一的**,这样你才能通过 `findViewById(R.id.noupdatetext)` 唯一找到一个控件。 --- ### 🕵️‍♂️ 查找重复的 `noupdatetext`: 你在 XML 中定义了多个 `TextView`,其中至少有两个使用了: ```xml android:id="@+id/noupdatetext" ``` 请检查你提供的 XML 文件中是否还有如下结构: ```xml <!-- 没有数据可更新 --> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/noupdate" ... > <TextView android:id="@+id/noupdatetext" ... /> </androidx.constraintlayout.widget.ConstraintLayout> ``` 然后你可能在其他地方(比如 `<layout>` 的开头注释部分)还有: ```xml <!-- 保留 layout 标签但不声明 data --> <layout ...> <!-- 某个隐藏的 TextView 或其他布局中也定义了 noupdatetext --> ``` 或者你可能在别的 `ConstraintLayout` 中误用了相同的 `id`。 --- ### ✅ 解决方法: #### ✅ 1. 确保每个 `id` 是唯一的 修改重复的 `@+id/noupdatetext` 为不同的 `id`。例如: ```xml <TextView android:id="@+id/noupdatetext1" ... /> ``` 或: ```xml <TextView android:id="@+id/noupdatetext2" ... /> ``` #### ✅ 2. 删除或修改重复的控件 如果你发现某个控件是多余的,可以直接删除它。 #### ✅ 3. 使用 `@android:id` 或已有的 `id`(不推荐) 除非你确实需要多个独立控件,否则不要重复使用 `@+id/xxx`。 --- ### ✅ 示例:修改后的唯一 ID 示例 ```xml <TextView android:id="@+id/noupdatetext" ... /> <!-- 另一个 TextView 不能再用 noupdatetext --> <TextView android:id="@+id/noupdatetext2" ... /> ``` --- ### ✅ 额外建议: - 使用 Android Studio 的 **“查找”功能**(Ctrl + F 或 Cmd + F)搜索 `noupdatetext`,快速定位重复项。 - 如果你使用了 `include` 或 `merge` 布局,也要检查被包含的布局中是否有重复 `id`。 - 使用 `@+id/xxx` 表示新建一个 id,使用 `@id/xxx` 表示引用已有的 id。 --- ### ✅ 总结: | 问题 | 原因 | 解决方案 | |------|------|----------| | Duplicate id `@+id/noupdatetext` | 同一个布局文件中重复定义了相同的 `id` | 修改为不同的 `id` 或删除重复控件 | ---
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值