Атрибут дубликата xml привязки данных Android

Недавно я начал разрабатывать приложение для Android, которое использует привязку данных. Моя проблема в том, что я не могу запустить приложение из-за этой ошибки:

Error:(10) Error parsing XML: duplicate attribute

Ошибка возникает в каждом файле, использующем привязку данных (я использую фрагменты). Гуглил часа 3 и не нашел решения.

построить.градле:

apply plugin: 'com.android.application'

android {
    dexOptions {
        preDexLibraries = false
        javaMaxHeapSize "2g"
    }
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    defaultConfig {
        applicationId "at.blacktasty.schooltoolmobile"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile files('libs/eneter-messaging-android-7.0.1.jar')
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.android.support:support-v4:23.4.0'
    testCompile 'junit:junit:4.12'
}

фрагмент_тестов.xml:

<layout 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"
    tools:context="layout.tests">

    <data>
        <variable
            name="deadline"
            type="at.blacktasty.schooltoolmobile.viewmodel.STViewModel"/>
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/list_tests"
            android:entries="@{deadline.deadline}"/>
    </LinearLayout>
</layout>

тесты.java:

package layout;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import at.blacktasty.schooltoolmobile.R;
import at.blacktasty.schooltoolmobile.databinding.FragmentSyncBinding;
import at.blacktasty.schooltoolmobile.databinding.FragmentTestsBinding;
import at.blacktasty.schooltoolmobile.viewmodel.STViewModel;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class tests extends Fragment {
    private STViewModel stViewModel;

    public tests() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        stViewModel = new STViewModel();
        FragmentTestsBinding binding = DataBindingUtil.inflate(
                inflater, R.layout.fragment_tests, container, false);
        View view = binding.getRoot();
        binding.setDeadline(stViewModel);


        return view;
    }
}

И файл xml, в котором возникает ошибка (debug\layout\fragment_tests.xml). layout_width и layout_height помечены как ошибка:

    <LinearLayout
        android:layout_width="match_parent" 
        android:layout_height="match_parent" android:tag="layout/fragment_tests_0" 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" tools:context="layout.tests">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list_tests"
        android:tag="binding_1"               />
</LinearLayout>

Я очень надеюсь, что кто-то может мне помочь.

РЕДАКТИРОВАТЬ: Здесь класс STViewModel:

public class STViewModel extends BaseObservable {
    private ObservableArrayList<Deadline> m_deadline = new ObservableArrayList<>();

    @Bindable
    public ObservableArrayList<Deadline> getDeadline(){
        return m_deadline;
    }

    public void setDeadline(ObservableArrayList<Deadline> value){
        m_deadline = value;
        notifyPropertyChanged(BR.deadline);
    }
}

person Blacktasty    schedule 18.10.2016    source источник
comment
у вас есть deadline внутри STViewModel?   -  person Ravi    schedule 18.10.2016
comment
Да, крайний срок находится внутри STViewModel, я добавил класс к своему вопросу.   -  person Blacktasty    schedule 18.10.2016
comment
попробуйте изменить его имя, это может решить вашу проблему   -  person Ravi    schedule 18.10.2016
comment
Я только что узнал, в чем проблема. Решение внизу в ответах.   -  person Blacktasty    schedule 18.10.2016
comment
Возможный дубликат Ошибка XML привязки данных Android   -  person user1046885    schedule 19.07.2017


Ответы (6)


Я только что узнал, что такое решение. Мне просто нужно было удалить layout_width и layout_height из определения <layout>.

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

вместо

<layout 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"
tools:context="layout.tests">
person Blacktasty    schedule 18.10.2016
comment
иногда вам нужно искать закрытие. решение всегда скрыто в проблеме. - person Nirmal; 21.10.2017
comment
В моем случае у меня было дубликат объявления xmlns:. - person Ajith Memana; 29.10.2018
comment
Работает на меня. Спасибо - person Khalid Taha; 29.01.2019
comment
В моем случае xmlns:android и xmlns:app, но спасибо. - person Pistos; 06.02.2019
comment
У меня тоже сработало - person Vishambar Pandey; 06.12.2019

Убедитесь, что xmlns:android не добавляется автоматически и к <layout>, и к вашему фактическому макету ViewGroup:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        ...>
    </android.support.v4.widget.DrawerLayout>
</layout>

Удалите xmlns:android из любого места.

person azizbekian    schedule 14.08.2018

Удалить линии

android:layout_width="match_parent"
android:layout_height="match_parent" 

под тегом layout в XML. Это приведет к ошибке сборки.

person Kanagalingam    schedule 15.03.2019

я использовал атрибут «xmlns» два раза, поэтому получал ошибку.

<?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:app="http://schemas.android.com/apk/res-auto">
    <android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/offwhite">

Исправлено с кодом ниже

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.ConstraintLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/offwhite">
person Abdul    schedule 17.10.2018

Это было решено путем удаления всех атрибутов из полей <layout>, т.е. путем сохранения его как

<layout>

  <data>

    <variable
        name=""
        type="" />

  </data>

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


  </LinearLayout>
</layout>
person Srikanth P    schedule 05.06.2018

Вы должны определить

android:orientation 

свойство для LinearLayout.

Ваш LinearLayout должен быть таким,

  <LinearLayout
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:tag="layout/fragment_tests_0" 
    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:orientation="vertical"
    tools:context="layout.tests">
   <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list_tests"
        android:tag="binding_1" />
  </LinearLayout>
person Ajith Pandian    schedule 18.10.2016