Blacktasty
Blacktasty

Reputation: 1115

Android Databinding xml duplicate attribute

I recently started developing on an android application which uses databinding. My problem now is that I can't run the app because of this error:

Error:(10) Error parsing XML: duplicate attribute

The error occurs in every file using databinding (I am using fragments). I googled for like 3 hours now and I can't find the solution.

build.gradle:

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'
}

fragment_tests.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>

tests.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;
    }
}

And the xml file where the error occurs (debug\layout\fragment_tests.xml). layout_width and layout_height are marked as error:

    <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>

I really hope someone can help me out.

EDIT: Here the STViewModel class:

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);
    }
}

Upvotes: 30

Views: 18233

Answers (6)

Kanagalingam
Kanagalingam

Reputation: 2214

Remove the lines

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

under your layout tag in xml. It will result in build error.

Upvotes: 12

Abdul
Abdul

Reputation: 238

i was using "xmlns" attrinute two time that is why was receiving error.

<?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">

Fixed with below code

<?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">

Upvotes: 5

azizbekian
azizbekian

Reputation: 62199

Make sure that xmlns:android isn't automatically added to both <layout> and your actual 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>

Remove xmlns:android from either place.

Upvotes: 13

Srikanth P
Srikanth P

Reputation: 1516

Its got solved by removing all attributes from <layout> fields, i.e by keeping it like

<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>

Upvotes: 3

Blacktasty
Blacktasty

Reputation: 1115

I've just found out what the solution is. I just had to delete layout_width and layout_height from the <layout> definition.

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

instead of

<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">

Upvotes: 102

Ajith Pandian
Ajith Pandian

Reputation: 1352

You should define

android:orientation 

property to LinearLayout.

Your LinearLayout should be like this,

  <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>

Upvotes: 0

Related Questions