You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android XML布局</LinearLayout>标签不匹配报错问题求解

Android布局XML标签嵌套不匹配错误修复方案

错误基础信息

开发Android应用编写布局XML文件时,在标注的</LinearLayout>位置触发语法错误,提示XML标签嵌套不匹配,原始问题代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
    <include layout="@layout/toolbar_layout" />
    <ScrollView android:background="@drawable/profpicback" android:layout_width="fill_parent" android:layout_height="fill_parent">
        <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
            <RelativeLayout android:gravity="center" android:id="@id/user_update_header" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="194.0dip">
                <RelativeLayout android:layout_width="135.29999dip" android:layout_height="135.29999dip">
                    <com.mobiroller.core.views.CircleImageView android:id="@id/user_update_profile_image" android:layout_width="133.29999dip" android:layout_height="133.29999dip" app:civ_border_color="@color/white" app:civ_border_width="4.0dip" app:srcCompat="@drawable/ic_person_white_24dp" />
                    <com.mobiroller.core.views.CircleImageView android:id="@id/user_update_open_gallery" android:layout_width="40.0dip" android:layout_height="40.0dip" android:src="@color/black" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" app:civ_border_color="@color/black" app:civ_fill_color="@color/black" />
                    <RelativeLayout>
                        <ImageView android:id="@id/user_update_image_edit" android:padding="6.0dip" android:layout_width="40.0dip" android:layout_height="40.0dip" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" app:srcCompat="@drawable/ic_camera_alt_white_24dp" />
            </RelativeLayout>
            <RelativeLayout android:id="@id/user_update_form" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@id/user_update_header">
                <androidx.recyclerview.widget.RecyclerView android:id="@id/user_update_list" android:background="#0000ffff" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="13.299988dip" />
            </RelativeLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

排查思路

  • 遵循XML语法的「后开先闭」嵌套规则,从根节点开始按缩进层级逐行核对开标签与闭合标签的配对关系,容器类标签(LinearLayout、RelativeLayout、ScrollView等)需要重点核对,自闭合标签(尾部带/的控件,比如ImageView、include、自定义控件)确认没有漏写结尾的/即可。
  • 从报错指向的标签位置向上回溯,统计所有未完成闭合的开标签数量,快速定位缺失闭合标签的层级。
  • 核对相邻层级标签的嵌套逻辑,确认不存在标签交叉嵌套的问题(比如A标签内嵌B标签,未闭合B就先闭合A的情况)。

产生原因

逐行核对标签配对关系可定位核心问题:嵌套层级中一共缺失2个RelativeLayout闭合标签,且现有闭合标签位置提前,导致嵌套顺序错乱:

  1. 包裹头像编辑ImageView的无属性<RelativeLayout>开标签之后,没有对应闭合标签。
  2. id为user_update_header、高度194dip的外层RelativeLayout开标签之后,没有对应闭合标签。
    当XML解析器运行到标注的</LinearLayout>位置时,标签栈中仍存在2个未闭合的RelativeLayout标签,因此触发嵌套不匹配报错。

修复方法

按照嵌套层级补全缺失的闭合标签,调整对应位置的代码缩进保证结构清晰,修复后可正常编译的完整代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <include layout="@layout/toolbar_layout" />
    <ScrollView
        android:background="@drawable/profpicback"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <RelativeLayout
                android:gravity="center"
                android:id="@id/user_update_header"
                android:visibility="gone"
                android:layout_width="fill_parent"
                android:layout_height="194.0dip">
                <RelativeLayout
                    android:layout_width="135.29999dip"
                    android:layout_height="135.29999dip">
                    <com.mobiroller.core.views.CircleImageView
                        android:id="@id/user_update_profile_image"
                        android:layout_width="133.29999dip"
                        android:layout_height="133.29999dip"
                        app:civ_border_color="@color/white"
                        app:civ_border_width="4.0dip"
                        app:srcCompat="@drawable/ic_person_white_24dp" />
                    <com.mobiroller.core.views.CircleImageView
                        android:id="@id/user_update_open_gallery"
                        android:layout_width="40.0dip"
                        android:layout_height="40.0dip"
                        android:src="@color/black"
                        android:layout_alignParentBottom="true"
                        android:layout_alignParentEnd="true"
                        app:civ_border_color="@color/black"
                        app:civ_fill_color="@color/black" />
                    <RelativeLayout
                        android:layout_width="40dip"
                        android:layout_height="40dip"
                        android:layout_alignParentBottom="true"
                        android:layout_alignParentEnd="true">
                        <ImageView
                            android:id="@id/user_update_image_edit"
                            android:padding="6.0dip"
                            android:layout_width="40.0dip"
                            android:layout_height="40.0dip"
                            app:srcCompat="@drawable/ic_camera_alt_white_24dp" />
                    </RelativeLayout>
                    <!-- 闭合135dip宽高的RelativeLayout -->
                </RelativeLayout>
                <!-- 闭合id为user_update_header的RelativeLayout -->
            </RelativeLayout>
            <RelativeLayout
                android:id="@id/user_update_form"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_below="@id/user_update_header">
                <androidx.recyclerview.widget.RecyclerView
                    android:id="@id/user_update_list"
                    android:background="#0000ffff"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_margin="13.299988dip" />
            </RelativeLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

注:修复时顺带给原本无属性的RelativeLayout补全了宽高和layout_align属性,和内部ImageView的布局逻辑匹配,可根据实际业务需求调整。


内容的提问来源于stack exchange,提问作者LENDED KILL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 20:36:33