我在android视图中经常出现问题,错误解析XML:第2行未绑定前缀。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:id="@+id/myScrollLayout" 
android:layout_width="fill_parent"  android:layout_height="wrap_content">
    <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" 
    android:text="Family" android:id="@+id/Family" 
    android:textSize="16px" android:padding="5px" 
    android:textStyle="bold" android:gravity="center_horizontal">
    </TextView>

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:orientation="vertical" android:scrollbars="vertical">
        <LinearLayout android:orientation="vertical" android:id="@+id/myMainLayout" 
        android:layout_width="fill_parent"  android:layout_height="wrap_content">
        </LinearLayout>
    </ScrollView>

</LinearLayout>

当前回答

对我来说,我在第一行得到了“unbound prefix”错误,尽管我在第四行拼错了android。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
anrdoid:fillViewport="true"
>

其他回答

如果您没有正确地包含xmlns:mm,则通常会出现此错误,它通常出现在代码的第一行。

对我来说…

xmlns:mm=“http://millennialmedia.com/android/schema”

我在第一行代码中漏掉了

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mm="http://millennialmedia.com/android/schema"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp"
android:background="@android:color/transparent" >

我也有同样的问题。

确保前缀(android:[whatever])的拼写和书写正确。对于这一行,xmlns:android="http://schemas.android.com/apk/res/android" 确保您有完整的前缀xmlns:android,并且拼写正确。与任何其他前缀相同-确保它们拼写正确,并有android:[name]。这就解决了我的问题。

好吧,这里有很多解决方案,但实际上没有解释问题的根本原因,所以我们开始:

当你看到像android:layout_width="match_parent"这样的属性时,android部分是前缀,这里属性的格式是prefix:NAME="VALUE"。在XML中,命名空间和前缀是避免命名冲突的方法,例如,我们可以有两个具有相同名称但不同前缀的不同属性,如:a:a="val"和b:a="val"。

要使用android或app或任何其他前缀,您应该使用XMLNS属性定义一个名称空间。

所以如果你有这个问题,只要找到没有定义名称空间的前缀,如果你有工具:…你应该添加工具命名空间,如果你有app:…属性,您应该将xmlns:app="http://schemas.android.com/apk/res-auto"添加到根元素

进一步阅读:

XML命名空间简单解释

W3中的XML命名空间

对我来说,我在第一行得到了“unbound prefix”错误,尽管我在第四行拼错了android。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
anrdoid:fillViewport="true"
>

此错误可能发生在使用未定义的前缀的情况下,例如:

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

<TabHost
    XYZ:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


</TabHost>

Android编译器不知道什么是XYZ,因为它还没有定义。

在您的情况下,您应该将以下定义添加到xml文件的根节点。

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