无法创建简单的数据绑定

时间:2016-03-21 06:33:20

标签: android data-binding

我在Android Studio中创建了Emtpy Project。接下来,我已将classpath 'com.android.databinding:dataBinder:1.0-rc2'添加到项目的build.gradle并将布局更改为:

<?xml version="1.0" encoding="utf-8"?>

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

    <data>
       <variable
          name="dataSource"
          type="com.example.demas.db1.DataSource" />
    </data>
    <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.demas.db1.MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!" />
    </RelativeLayout>
</layout>

现在我无法构建应用程序并出现错误:

03-21 02:19:31.692 6822-6822/com.example.demas.db1 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.example.demas.db1, PID: 6822
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demas.db1/com.example.demas.db1.MainActivity}: android.view.InflateException: Binary XML file line #3: Binary XML file line #3: Error inflating class layout
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)

这是我的DataSource类:

package com.example.demas.db1;

public class DataSource {
    private String name;

    public static DataSource get(String name) {
        return new DataSource(name);
    }

    public DataSource(String name) {
        this.name = name;
    }

    public String getMessage() {
        return String.format("Hello, %s!", name);
    }

}

我该如何解决?

1 个答案:

答案 0 :(得分:1)

我已从socket.emit

中移除tools:context="com.example.demas.db1.MainActivity"来解决错误