安卓数据绑定XML错误

9

我正在一个Android Studio项目中使用数据绑定库,但每当我构建、运行、清除、重建等操作时,都会出现以下错误:

 :app:processDebugResources AGPBI:

{"kind":"error","text":"Error parsing XML: duplicate attribute","sources": [{"file":"C:\\Users\\lucia.beltran\\Desktop\\Picho\\Projects\\Personal\\ improved-tribble\\ImprovedTribble\\app\\build\\intermediates\\data-binding-layout-out\\debug\\ layout\\task_list_item.xml","position":{"startLine":16}}],"original":"","tool":"AAPT"}

C:\Users\lucia.beltran\Desktop\Picho\Projects\Personal\improved-tribble\ImprovedTribble\app\build\intermediates\data-binding-layout-out\debug\layout\task_list_item.xml:17: error: Error parsing XML: duplicate 

 FAILED

FAILURE: Build failed with an exception.

* What went wrong: Execution failed for task ':app:processDebugResources'.
> com.android.ide.common.process.ProcessException: Failed to execute aapt

* Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 3.826 secs

我的布局如下:

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

    <data>
        <variable
            name="task"
            type="com.pichardo.improvedtribble.models.Task" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

    </LinearLayout>

</layout>

而Gradle控制台所显示的绑定文件布局如下:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" android:tag="layout/task_list_item_0" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">

</LinearLayout>

我知道重复属性是什么,但不知道为什么会崩溃。
我读了这个问题,但在我的build.gradle文件中没有类似的东西。
有什么建议吗?

你应该使用Ctrl+K或Cmd+K来正确格式化你的源代码块。 - Eugene Brusov
2个回答

14

尝试从 layout 标签中删除 android:layout_widthandroid:layout_height="match_parent"

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

    <data>
        <variable
            name="task"
            type="com.pichardo.improvedtribble.models.Task" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

    </LinearLayout>

</layout>

是的,我不知道为什么我没有想到,谢谢。 - J. Pichardo
1
这是真的,有时候会发生。 - Mohit Suthar
您应该使用Ctrl+K或Cmd+K来正确格式化您的源代码块。 - Eugene Brusov

6
以下是我编辑过的真实代码:
<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
>

    <data>
        <variable
            name="MyDTO"
            type="com.example.MyDTO"
        />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:mContext="com.example.MyActivity"
    >

所有命名空间都必须在外部布局标记中。任何删除操作都没有起作用。

1
所有的命名空间必须在外部布局标签中 - 是的。否则你的项目将无法编译。 - Johnny Five

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接