Android数据绑定:无法解析符号

Lon*_*ger 16 java data-binding android gradle

我试图在android studio中使用beta功能(数据绑定).按照android studio的指南,我可以在android studio中找到相关的类DataBindingInfo.但是在创建项目后,数据绑定类不会生成.有人可以帮忙吗?

app模块的build.gradle

apply plugin: 'com.android.application'

apply plugin: 'com.android.databinding'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.example.pigfamily.myapplication"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
}
Run Code Online (Sandbox Code Playgroud)

该项目的build.gradle

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

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        dependencies {
            classpath "com.android.tools.build:gradle:1.3.0"
            classpath "com.android.databinding:dataBinder:1.0-rc1"
        }
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}
Run Code Online (Sandbox Code Playgroud)

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="user"
            type="com.example.pigfamily.myapplication.User" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
 >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.name}" />

    </LinearLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)

MainActivity.java

package com.example.pigfamily.myapplication;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActivityMainBinding //cannot resolve the symbol here
    }

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

小智 38

首先,在build.gradle文件中启用dataBinding

android {
    ...
    dataBinding{
        enabled=true
    }
}
Run Code Online (Sandbox Code Playgroud)


ten*_*int 18

我有同样的问题.我正在挖掘gradle设置,清理,重建......没有任何效果.最后我要做的就是重启Android Studio

https://www.bignerdranch.com/blog/descent-into-databinding/

在撰写本文时,这种集成需要一点启动才能开始.要在添加标记后使ListItemCrimeBinding可用,您必须重新启动Android Studio,然后重建项目.


Eri*_*ric 9

要实现绑定,请确保:

  1. 在 中app.gradle,启用dataBinding

     apply plugin: 'com.android.application'
     ...
    
     android {
         ...
         buildFeatures {
             dataBinding true
         }
         ...
     }
    
     dependencies {
         ...
     }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将现有的 XML 布局包装在一个<layout>元素中:

     <layout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         xmlns:tools="http://schemas.android.com/tools">
    
         <!-- note: xmlns above only needed if used in original XML -->
         <!-- original XML here -->
    
     </layout>
    
    Run Code Online (Sandbox Code Playgroud)
  3. Activity/中Fragment,使用绑定

     // inflating xml_layout_name.xml...
     val binding = XmlLayoutNameBinding.inflate(LayoutInflater.from(context))
    
    Run Code Online (Sandbox Code Playgroud)
  4. if 无法解析该类XxxBinding(即使它可以解析XxxBindingImpl

    1. 首先尝试使用标签更改任何 XML 布局中的字符
    2. 如果这不起作用,请重新启动Android Studio、同步并重建项目