【问题标题】:error Unexpected namespace prefix "xmlns" found for tag LinearLayout错误为标签 LinearLayout 找到了意外的命名空间前缀“xmlns”
【发布时间】:2013-07-04 17:47:23
【问题描述】:

我在 LinearLayout 的 "xmlns:android="http://schemas.android.com/apk/res/android" 中遇到错误 "Unexpected namespace prefix "xmlns" found for tag LinearLayout"。

    <?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
            android:background="@drawable/wallpaper"
    android:layout_height="match_parent" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>
    </ScrollView>

【问题讨论】:

标签: android xml


【解决方案1】:

没有必要为您在layout.xml 文件中定义的每个布局放置namespace 属性。命名空间只能为根级元素定义如下:

 <?xml version="1.0" encoding="utf-8"?>
 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
        android:background="@drawable/wallpaper"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>
</ScrollView>

【讨论】:

  • 可能不需要,但完全合法。
  • @omer341 你找到解决办法了吗??
  • @MichaelKay 鉴于这是一个关于 Android 的问题,您确定它对于 Android 的 xml 解析器“完全合法”吗?这解决OP的情况。我尝试查找有关此的官方文档(W3 规范和 Android 特定提及),但结果很短。
  • @MichaelKay developer.android.com/reference/javax/xml/namespace/… "前缀只能绑定到当前范围内的单个命名空间 URI" 也许 Android Lint 只检查前缀是否只声明一次,并且不检查值是否相同。
  • @atalum 您引用的是 API 规范而不是 XML 规范。如果 Android 解析器严重不符合 XML 规范,我相信我们会听说的。
【解决方案2】:

删除这一行

xmlns:android="http://schemas.android.com/apk/res/android"

来自您的LinearLayout。您只需在根目录layout 中添加一次。每个 xml 中只能定义一次命名空间

【讨论】:

  • 错,错,错。您可以根据需要在 XML 中定义命名空间。
【解决方案3】:

XML 似乎格式正确。您也许可以通过更改 XML 来规避这些问题,但这并不能解释为什么会出现错误。我认为我们需要更多地了解您如何解析 XML。检查其他 XML 解析器是否接受它。

【讨论】:

    猜你喜欢
    • 2013-02-01
    • 2013-05-28
    • 2013-06-01
    • 2014-04-05
    • 2013-05-03
    • 2013-09-11
    • 2014-02-16
    • 2016-12-18
    • 2013-05-06
    相关资源
    最近更新 更多