示例Android数据绑定错误

时间:2016-03-04 06:09:48

标签: java android android-layout android-gradle android-databinding

我正在尝试在我的一个Android项目中实现数据绑定。我无法在网络中找到完整的工作示例,因此按照以下步骤开始:http://developer.android.com/tools/data-binding/guide.html

我的文件中的代码如下:

build.gradle(project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

build.gradle(module)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "org.my.anddatabind"
        minSdkVersion 18
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
}

MainActivity.java

package org.my.anddatabind;

import android.app.Activity;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import org.my.anddatabind.databinding.MainActivityBinding;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
        User user = new User("Test", "User");
        binding.setUser(user);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
    <variable name="user" type="com.my.anddatabind.User"/>
</data>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{user.firstName}"/>
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{user.lastName}"/>
</LinearLayout>
</layout>

User.java

package org.my.anddatabind;

public class User {
    public final String firstName;
    public final String lastName;
    public User(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

当我构建项目时,我收到错误:

**** /数据绑定错误****消息:无法解析用户〜文件的类型:C:\ Users \ xxx \ AndroidStudioProjects \ AndDataBind \ app \ src \ main \ res \ layout \ main_activity.xml loc :11:24 - 11:27 **** \数据绑定错误****

MainActivity.java中的setUser(user)也显示错误:setUser(&#39; org.my.anddatabind.User&#39;)&#39;&#39;&#39;不能应用于&#39;(org.my.anddatabind.User)&#39;

1 个答案:

答案 0 :(得分:1)

请检查您的课程包名称。 您的“用户”类包名称似乎是

org.my.anddatabind.User

但在xml中,您已将类型定义为

<data>
  <variable name="user" type="`com.my.anddatabind.User`"/>
</data>

com.my.anddatabind.User