简体   繁体   English

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

[英]Android Data binding : Cannot resolve symbol

I have tried to use the beta features(data binding) in the android studio.我曾尝试在 android studio 中使用 beta 功能(数据绑定)。 After following the guides from the android studio, I can find the related class DataBindingInfo in the android studio.按照android studio的指南,我可以在android studio中找到相关的类DataBindingInfo。 But the databinding class does not generate after I create the project.但是在我创建项目后不会生成数据绑定类。 Can someone help?有人可以帮忙吗?

build.gradle for the app module应用程序模块的 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 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>

MainActivity.java 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);
    }
}

enable dataBinding in your App Level build.gradle file在您的App Level build.gradle 文件中启用 dataBinding

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

I had this same problem.我有同样的问题。 I was digging through gradle settings, cleaning, rebuilding... nothing worked.我正在挖掘 gradle 设置,清理,重建......没有任何效果。 Finally all I had to do was restart Android Studio最后我要做的就是重启 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,然后重新构建项目。

to implement binding, make sure that:要实现绑定,请确保:

  1. in the app.gradle , enable dataBinding :app.gradle中,启用dataBinding

     apply plugin: 'com.android.application' ... android { ... dataBinding { enabled = true } ... } dependencies { ... }
  2. wrap the existing XML layout in a <layout> element:将现有的 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. in Activity / Fragment , use the BindingActivity / Fragment中,使用 Binding

     val binding = XmlLayoutNameBinding.inflate(context.layoutInflater) // use the layout
  4. if is unable to resolve the XxxBinding class (even though it can resolve XxxBindingImpl ) if 无法解析XxxBinding类(即使它可以解析XxxBindingImpl

    1. first try changing a character in ANY XML layout with the tag首先尝试使用标记更改任何 XML 布局中的字符
    2. if that doesn't work, restart Android Studio , sync & rebuild project如果这不起作用,请重新启动Android Studio ,同步和重建项目

Add on build.gradle(:app) viewBinding true添加 build.gradle(:app) viewBinding true

android {
  buildFeatures {
        viewBinding true
    }

}

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

If clear Project and Rebuild Project don't work for you try the simple solution.如果clear ProjectRebuild Project对您不起作用,请尝试简单的解决方案。 It sometimes doesn't work when you clone a project from remote git branches .You need to just close your project and delete the .gradle and .idea folder of your android project.当您从remote git branches克隆项目时,它有时不起作用。您只需关闭您的项目并删除您的 android 项目的.gradle.idea文件夹。 Then reopen the project.然后重新打开项目。

For me I had to add:对我来说,我必须补充:

buildFeatures {
    viewBinding true
}

to my build.gradle file, then run File->Sync Project With Gradle Files到我的build.gradle文件,然后运行File->Sync Project With Gradle Files

In my case, my layout file had some errors and therefore AS was not creating the binding class.就我而言,我的布局文件有一些错误,因此 AS 没有创建绑定类。 I could find it by expecting the XML file and seeing the error messages.我可以通过期待 XML 文件并查看错误消息来找到它。

be sure you add data binding in your build.gradle确保在 build.gradle 中添加数据绑定

and after that sync project after that Restart and clear caches from the file in android studio .并在该同步项目之后重新启动并从android studio中的文件中清除缓存 this way solve my problem这种方式解决了我的问题

For my case I added :对于我的情况,我补充说:

         dataBinding {
                  enabled=true
         }

Then went to File > Settings > Gradle > Untick offline work> Apply> Ok Then cleaned my project.然后去 File > Settings > Gradle > Untick offline work > Apply > Ok 然后清理我的项目。

I've seen this answer nowhere so I suggest: if you added some activity from context menu by clicking on some package like 'data' or 'ui', Android Studio saved path to binding like so: import com.damian.yourappname.ui.databinding.ActivitySearchBinding;我在任何地方都没有看到这个答案,所以我建议:如果您通过单击“data”或“ui”之类的包从上下文菜单中添加了一些活动,Android Studio 会保存绑定路径,如下所示:import com.damian.yourappname.ui .databinding.ActivitySearchBinding;

I tested it a few times with my previous projects and it occurs only when sample activity like ScrollingActivity is added to a package other package for all classes.我在以前的项目中对其进行了几次测试,并且仅在将诸如 ScrollingActivity 之类的示例活动添加到所有类的其他包的包中时才会发生这种情况。 import com.damian.yourappname.databinding.ActivitySearchBinding;导入 com.damian.yourappname.databinding.ActivitySearchBinding;

So best way is just creating sample activity in root folder and after gradle sync moving to some package所以最好的方法是在根文件夹中创建示例活动,然后在 gradle 同步移动到某个包之后

I found a way without restart or invalidate cache.我找到了一种无需重新启动或使缓存无效的方法。 I thing this because binding need to restart or something, so I re-write private ActivityMainBinding binding;我之所以这样做是因为绑定需要重新启动什么的,所以我重新编写了private ActivityMainBinding binding; and viewBinding work again.和 viewBinding 再次工作。

I don't know why this work but this save my time than restart/invalidate cache我不知道为什么会这样,但这比重新启动/无效缓存节省了我的时间

In my case i deleted build, build-cache folders in project scope, then build folder in app module.在我的情况下,我删除了项目范围内的 build、build-cache 文件夹,然后在 app 模块中删除了 build 文件夹。 Afterwards restarted PC and it started working之后重新启动PC并开始工作

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更新:清理并重建项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM