我正在使用Android Studio 2.0 Preview 4。 我正在使用Android SDK工具25 rc1。无论清理/重建项目多少次,此错误都会持续存在。 File-> Invalidate Caches并重启也不起作用。 我无法运行最基本的数据绑定示例。
build.gradle文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.chiragshenoy.myapplication"
minSdkVersion 15
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'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
}
MainActivity.java
package com.example.chiragshenoy.myapplication;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MainActivity binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
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;
}
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.chiragshenoy.myapplication.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>
这是我的顶级build.gradle文件
// 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:2.0.0-alpha3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
答案 0 :(得分:151)
如果你的gradle版本没问题(1.5+)那么你应该试试这个:
这可能会解决问题。请告诉我们。
答案 1 :(得分:23)
此解决方案适用于我“文件 - &gt;无效缓存/重启” https://stackoverflow.com/a/32191257/2205809
答案 2 :(得分:5)
有一个简单的解决方案。
希望这个答案对您有帮助。
答案 3 :(得分:4)
您应该使用:
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
在onCreate
MainActivity不是生成的Binding类。
答案 4 :(得分:4)
答案 5 :(得分:3)
我使用的最快的黑客之一是将xml文件重命名为。
e.g。
将abc_layout.xml
重命名为abc_layout2.xml
。绑定导入显示错误后,将其重命名为abc_layout.xml
。
重命名的捷径是shift + F6
。整个项目重建的方式更快。这只会从一个文件中删除错误。
答案 6 :(得分:3)
默认情况下,将根据布局文件的名称生成Binding
类,将其转换为Pascal大小写,并为其添加“Binding”后缀。上面的布局文件是 main_activity.xml ,因此生成类是 MainActivityBinding 。问题是您正在尝试手动创建绑定类。
此类包含布局属性(例如用户变量)到布局Views
的所有绑定,并知道如何为绑定表达式赋值。创建绑定的最简单方法是在膨胀时执行此操作。
无需重启Android Studio。
答案 7 :(得分:2)
在顶级Build.Gradle文件中使用gradle 1.5.0或更高版本
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
}
如果低于1.5.0,则在顶级gradle文件中使用此文件
classpath "com.android.databinding:dataBinder:1.0-rc4"
答案 8 :(得分:0)
只需尝试使缓存无效/从文件重新启动。这对我有用。
答案 9 :(得分:0)
Migrating my existing project to AndroidX之后,我也遇到了同样的问题,我通过做下面的事情来解决了这个问题
Build -> Clean Project
Build -> Rebuild Project
之后
File -> Invalidate Caches/Restart...
答案 10 :(得分:0)
检查绑定类的名称,此处 ActivityMainInitBinding 是基于xml文件 activity_mail_init.xml 生成的。如果使用错误的类,则会弹出错误。
val binding : ActivityMainInitBinding=
DataBindingUtil.setContentView(this, R.layout.activity_main_init)
答案 11 :(得分:0)
迁移到AndoridX之后,您需要从XMl中删除旧的v4或v7小部件。例如,如果您正在使用某些AppCompat小部件,例如-p,则需要替换
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:tag="showMore"/>
用andoridx小部件替换它
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/txt_summary_desc_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="Read More"/>
对我来说很好。
答案 12 :(得分:0)
如果刷新继续产生错误,则没有任何用处。 当出现这些错误时,很可能在您的xml文件中存在编译器在创建布局时没有显示给您的问题。 实际上,可能会发生此错误,仅在其“ debug”文件夹中可见。如果应该发生这种类型的错误,只需遵循debug文件夹中的错误并从原始文件中进行更正。 就我而言,错误是两个重复的
xmlns:app =“ http://schemas.android.com/apk/res-auto”
我看不到
答案 13 :(得分:0)
删除模块级别build
文件夹,然后重新打开您的项目。
答案 14 :(得分:0)
我收到此错误,是因为我错过了xml视图中的标记,这里不是这种情况。
答案 15 :(得分:0)
我通过用<layout>
标签包围xml布局文件来解决此错误
your_layout.xml
文件应类似于
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/view_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</layout>
这对我有用。