You need to enable JavaScript to run this app. Android Studio开发导航抽屉时布局XML报AAPT not well-formed错误
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio开发导航抽屉时布局XML报AAPT not well-formed错误

报错根源定位

从提供的布局代码片段和报错行(19行,即include标签所在行)的位置,可直接定位到两处导致XML格式非法的书写问题:

  • 父级<LinearLayout>开始标签未完成闭合
    写完android:orientation="vertical"属性后,直接换行编写后续代码,漏掉了标签收尾的>符号。XML解析器遇到未闭合的开标签时,会顺延将下一行的独立标签判定为非法token,这也是为什么报错位置指向include标签行的核心原因。
  • <include>标签的layout属性缺失强制要求的命名空间前缀
    当前书写的属性为layout="@layout/main_toolbar",AAPT解析布局时要求所有系统内置属性必须带android:命名空间前缀,正确写法为android:layout="@layout/main_toolbar"。无命名空间的裸属性会被判定为非法格式字符,直接触发编译报错。
修正后的可编译布局片段
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:layout="@layout/main_toolbar"/>

    <RelativeLayout
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:background="@color/achtergrond">
同类AAPT XML报错快速排查方案
  • 不直接依赖报错行号定位问题:遇到"not well-formed (invalid token)"类XML语法错误时,优先从报错行向上回溯2-3行,检查父标签是否正确闭合、属性是否漏写命名空间前缀。
  • 布局内所有系统控件属性,必须严格带上android:前缀;自定义属性需对应引入项目自定义命名空间,不可省略前缀直接书写属性名。
  • 借助Android Studio内置代码校验能力:全选布局文件代码后按Ctrl+Alt+L触发自动格式化,IDE会高亮标出所有格式不合法的位置,大幅降低手动排查成本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.18 04:06:04