Android Databinding xml重复属性

Bla*_*sty 22 xml data-binding android android-layout android-databinding

我最近开始开发一个使用数据绑定的android应用程序.我现在的问题是因为这个错误我无法运行应用程序:

Error:(10) Error parsing XML: duplicate attribute
Run Code Online (Sandbox Code Playgroud)

使用数据绑定(我正在使用片段)在每个文件中发生错误.我用Google搜索了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'
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

以及发生错误的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>
Run Code Online (Sandbox Code Playgroud)

我真的希望有人可以帮助我.

编辑:这里是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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Bla*_*sty 75

我刚刚发现了解决方案.我只需要从<layout>定义中删除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">
Run Code Online (Sandbox Code Playgroud)

代替

<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">
Run Code Online (Sandbox Code Playgroud)

  • 就我而言,我有重复的xmlns:声明。 (2认同)
  • 在我的情况下,`xmlns:android` 和 `xmlns:app`,但谢谢。 (2认同)

azi*_*ian 13

确保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>
Run Code Online (Sandbox Code Playgroud)

xmlns:android从任何一个地方删除.


Kan*_*gam 12

删除线条

android:layout_width="match_parent"
android:layout_height="match_parent" 
Run Code Online (Sandbox Code Playgroud)

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


Abd*_*dul 5

我两次使用“ 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">
Run Code Online (Sandbox Code Playgroud)

用下面的代码固定

<?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">
Run Code Online (Sandbox Code Playgroud)