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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32815608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:08:50  来源:igfitidea点击:

Android Data binding : Cannot resolve symbol

javaandroiddata-bindinggradle

提问by Long Ranger

I have tried to use the beta features(data binding) in the android studio. After following the guides from the android studio, I can find the related class DataBindingInfo in the android studio. But the databinding class does not generate after I create the project. Can someone help?

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

build.gradle for the app module

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

build.gradle for the project

项目的 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()
    }
}

activity_main.xml

活动_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>

MainActivity.java

主活动.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);
    }
}

回答by tenprint

I had this same problem. I was digging through gradle settings, cleaning, rebuilding... nothing worked. Finally all I had to do was restart Android Studio

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

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

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

As of this writing, this integration needs a little jump-start to get going. To make ListItemCrimeBinding available after adding the tag, you must restart Android Studio, then rebuild the project.

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

回答by Ethan Reinsch

Either click sync if a dialogue pops up, hit the sync button next to save, or restart Android Studio.

如果弹出对话框,请点击同步,点击旁边的同步按钮保存,或重新启动 Android Studio。

回答by Kishore Kittu

enable dataBinding in your App Levelbuild.gradle file

在您的App Levelbuild.gradle 文件中启用dataBinding

android {
    ...
    dataBinding{
        enabled=true
    }
}

回答by Rohan Lodhi

1.Add Below in app gradle

1.在app gradle下面添加

 dataBinding {
        enabled = true
    }

2.In xml layout write below code

2.在xml布局中写下面的代码

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

UPDATED: CLEAN AND REBUILD THE PROJECT

更新:清理并重建项目

回答by Eric

to implement binding, make sure that:

要实现绑定,请确保:

  1. in the app.gradle, enable dataBinding:

    apply plugin: 'com.android.application'
    ...
    
    android {
        ...
        dataBinding {
            enabled = true
        }
        ...
    }
    
    dependencies {
        ...
    }
    
  2. wrap the existing XML layout in a <layout>element:

    <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>
    
  3. in Activity/Fragment, use the Binding

    val binding = XmlLayoutNameBinding.inflate(context.layoutInflater)
    // use the layout
    
  4. if is unable to resolve the XxxBindingclass (even though it can resolve XxxBindingImpl) restart Android Studio, sync & rebuild project

  1. app.gradle,启用dataBinding

    apply plugin: 'com.android.application'
    ...
    
    android {
        ...
        dataBinding {
            enabled = true
        }
        ...
    }
    
    dependencies {
        ...
    }
    
  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>
    
  3. Activity/ 中Fragment,使用绑定

    val binding = XmlLayoutNameBinding.inflate(context.layoutInflater)
    // use the layout
    
  4. 如果无法解析XxxBinding类(即使它可以解析XxxBindingImpl重新启动Android Studio,同步和重建项目

回答by LeonardoSibela

In my case, my layout file had some errors and therefore AS was not creating the binding class. I could find it by expecting the XML file and seeing the error messages.

就我而言,我的布局文件有一些错误,因此 AS 没有创建绑定类。我可以通过期待 XML 文件并查看错误消息来找到它。

回答by sana ebadi

be sure you add data binding in your build.gradle

确保在 build.gradle 中添加数据绑定

and after that syncproject after that Restart and clear cachesfrom the file in android studio. this way solve my problem

和后同步,经过项目重新启动和清除缓存从文件android studio。这样就解决了我的问题

回答by Gk Mohammad Emon

If clear Projectand Rebuild Projectdon't work for you try the simple solution. It sometimes doesn't work when you clone a project from remote git branches.You need to just close your project and delete the .gradleand .ideafolder of your android project. Then reopen the project.

如果clear ProjectRebuild Project没有为你工作尝试简单的解决方案。当您从remote git branches.clone项目时,它有时不起作用。您只需要关闭您的项目并删除您的 android 项目的.gradle.idea文件夹。然后重新打开项目。

回答by user8420228

For my case I added :

对于我的情况,我添加了:

         dataBinding {
                  enabled=true
         }

Then went to File > Settings > Gradle > Untick offline work> Apply> Ok Then cleaned my project.

然后去文件>设置> Gradle>取消离线工作>应用>确定然后清理我的项目。