2016-10-18 28 views
8

Gần đây tôi đã bắt đầu phát triển trên một ứng dụng Android sử dụng databinding. Vấn đề của tôi bây giờ là tôi không thể chạy ứng dụng vì lỗi này:Android Databinding xml duplicate attribute

Error:(10) Error parsing XML: duplicate attribute 

Lỗi xảy ra trong mỗi tệp sử dụng databinding (Tôi đang sử dụng phân đoạn). Tôi googled cho như 3 giờ bây giờ và tôi không thể tìm thấy giải pháp.

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

Và file xml xảy ra lỗi (debug \ layout \ fragment_tests.xml). layout_width và layout_height được đánh dấu là lỗi:

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

Tôi thực sự hy vọng ai đó có thể giúp tôi.

EDIT: Đây là lớp 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); 
    } 
} 
+0

làm bạn có 'deadline' bên 'STViewModel'? –

+0

Có, thời hạn ở bên trong STViewModel, tôi đã thêm lớp vào câu hỏi của mình. – Blacktasty

+0

thử thay đổi tên của nó, nó có thể giải quyết vấn đề của bạn –

Trả lời

31

Tôi vừa mới phát hiện ra những giải pháp là. Tôi chỉ cần xóa layout_width và layout_height từ định nghĩa <layout>.

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

thay vì

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

hoạt động như một nét duyên dáng –

+0

đôi khi bạn cần nhìn đóng cửa .solution luôn ẩn trong vấn đề. – Nirmal

0

Bạn nên xác định

android:orientation 

tài sản để LinearLayout.

LinearLayout của bạn nên được như thế này,

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

Thật không may lỗi vẫn còn – Blacktasty