错误:(8)解析XML时出错:未绑定前缀[duplicate]

问题描述 投票:0回答:2

这个问题在这里已有答案:

我无法弄清楚为什么我会收到此错误。从一个快速的谷歌搜索,我可以弄清楚是有一些语法错误,但我仍然无法弄明白。

enter image description here

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent">

       <Button
           android:id="@+id/but1"
           android:layout_width="100dp"
           android:layout_height="200dp"
           android:text="button1"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/but2"
        android:text="button2"/>
    </LinearLayout>


    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>
java android xml
2个回答
0
投票

必须声明命名空间前缀(app中的app:layout_constraintBottom_toBottomOf)(绑定到命名空间URI)。您的错误表明它不是。

声明表单的名称空间和名称空间声明

xmlns:xyz="http://example.com/something"

你想要申报的共同祖先。

您从中复制XML的模板应该已经有了这样的声明。如果您再也找不到它,可以查看文档或谷歌搜索示例。在这种情况下,app:layout_constraintBottom_toBottomOf似乎应声明如下:

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

您应该对任何其他已使用但未定义的名称空间前缀重复此过程:

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

等等


0
投票

尝试在下面的代码中添加xmlns:app="http://schemas.android.com/apk/res-auto"

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" //change here
android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent">

   <Button
       android:id="@+id/but1"
       android:layout_width="100dp"
       android:layout_height="200dp"
       android:text="button1"/>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/but2"
    android:text="button2"/>
</LinearLayout>


<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

© www.soinside.com 2019 - 2024. All rights reserved.