【问题标题】:Android Databinding xml duplicate attributeAndroid Databinding xml重复属性
【发布时间】:2017-02-27 10:56:27
【问题描述】:

我最近开始开发一个使用数据绑定的 android 应用程序。我现在的问题是由于此错误而无法运行该应用程序:

Error:(10) Error parsing XML: duplicate attribute

使用数据绑定的每个文件中都会发生错误(我正在使用片段)。我现在用谷歌搜索了大约 3 个小时,但找不到解决方案。

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

以及发生错误的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);
    }
}

【问题讨论】:

  • 你在STViewModel里面有deadline吗?
  • 是的,截止日期在 STViewModel 内,我已将课程添加到我的问题中。
  • 尝试改名,或许能解决您的问题
  • 我刚刚发现了问题所在。解决方案在答案下方。

标签: android xml android-layout data-binding android-databinding


【解决方案1】:

我刚刚找到了解决方案。我只需要从 &lt;layout&gt; 定义中删除 layout_width 和 layout_height。

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

【讨论】:

  • 有时你需要看看闭包。解决方案总是隐藏在问题中。
  • 就我而言,我有重复的 xmlns: 声明。
  • 为我工作。谢谢
  • 就我而言,xmlns:androidxmlns:app,但谢谢。
  • 也为我工作
【解决方案2】:

确保xmlns:android 不会自动添加到&lt;layout&gt; 和您的实际布局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

【讨论】:

    【解决方案3】:

    删除线

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

    在 xml 中的 layout 标记下。这将导致构建错误。

    【讨论】:

      【解决方案4】:

      我两次使用“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">
      

      【讨论】:

        【解决方案5】:

        通过从 &lt;layout&gt; 字段中删除所有属性来解决它,即保持它喜欢

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

        【讨论】:

          【解决方案6】:

          你应该定义

          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>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-02-04
            • 2016-04-18
            • 1970-01-01
            • 1970-01-01
            • 2012-06-30
            • 2017-02-12
            相关资源
            最近更新 更多