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"
xmlns:tools="http://schemas.android.com/tools"
:appxmlns="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
...
</LinearLayout>
-
在 Android 开发中,构建项目时,上述 XML 片段出错,出现如下错误信息
Cause: com.ctc.wstx.exc.WstxUnexpectedCharException:
Unexpected character ':' (code 58) (missing namespace prefix?)
at [row,col {unknown-source}]: [5,5]). Check logs for more details.
问题原因
-
:appxmlns是非法的 XML 属性名,XML 属性名不能以冒号:开头,除非它是命名空间前缀,例如,xmlns:app -
这里的 :appxmlns 被解析器当作一个带前缀的属性,但前缀为空,即冒号前没有内容,所以报错
Unexpected character ':' (code 58) (missing namespace prefix?)
-
而且,这是一行重复且错误的命名空间声明,正确的命名空间已在上方声明
xml
xmlns:app="http://schemas.android.com/apk/res-auto"
处理策略
- 删除错误的那一行,保留正确的命名空间声明即可
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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
...
</LinearLayout>