繁体   English   中英

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

[英]Android Data binding : Cannot resolve symbol

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

应用程序模块的 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

// 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

<?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

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);
    }
}

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

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

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

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

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

要实现绑定,请确保:

  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中,使用 Binding

     val binding = XmlLayoutNameBinding.inflate(context.layoutInflater) // use the layout
  4. if 无法解析XxxBinding类(即使它可以解析XxxBindingImpl

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

添加 build.gradle(:app) viewBinding true

android {
  buildFeatures {
        viewBinding true
    }

}

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

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

对我来说,我必须补充:

buildFeatures {
    viewBinding true
}

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

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

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

并在该同步项目之后重新启动并从android studio中的文件中清除缓存 这种方式解决了我的问题

对于我的情况,我补充说:

         dataBinding {
                  enabled=true
         }

然后去 File > Settings > Gradle > Untick offline work > Apply > Ok 然后清理我的项目。

我在任何地方都没有看到这个答案,所以我建议:如果您通过单击“data”或“ui”之类的包从上下文菜单中添加了一些活动,Android Studio 会保存绑定路径,如下所示:import com.damian.yourappname.ui .databinding.ActivitySearchBinding;

我在以前的项目中对其进行了几次测试,并且仅在将诸如 ScrollingActivity 之类的示例活动添加到所有类的其他包的包中时才会发生这种情况。 导入 com.damian.yourappname.databinding.ActivitySearchBinding;

所以最好的方法是在根文件夹中创建示例活动,然后在 gradle 同步移动到某个包之后

我找到了一种无需重新启动或使缓存无效的方法。 我之所以这样做是因为绑定需要重新启动什么的,所以我重新编写了private ActivityMainBinding binding; 和 viewBinding 再次工作。

我不知道为什么会这样,但这比重新启动/无效缓存节省了我的时间

在我的情况下,我删除了项目范围内的 build、build-cache 文件夹,然后在 app 模块中删除了 build 文件夹。 之后重新启动PC并开始工作

1.在app gradle下面添加

 dataBinding {
        enabled = true
    }

2.在xml布局中写下代码

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

更新:清理并重建项目

暂无
暂无

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

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