我正在学习有关数据绑定的知识,我尝试了3次,并且完全按照教程进行操作,但是在运行应用程序时,我不断收到错误“错误:找不到符号类ActivityMainBindingImpl” ,
它会用ActivityMainBindingImpl
生成一个Java文件,我的SDK中应该有问题吗?
主要:
private ActivityMainBinding activityMainBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
activityMainBinding.setPerson(getCurrentPerson());
}
public Person getCurrentPerson(){
Person person = new Person();
person.setName("lance");
person.setLastName("claudio");
return person;
}
型号:
public class Person {
private String name;
public Person(String name, String lastName) {
this.name = name;
this.lastName = lastName;
}
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
private String lastName;
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="person"
type="com.pedrovs.databindingexample.Person" />
</data>
<LinearLayout 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"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{person.setName()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{person.setLastName()}">
</TextView>
</LinearLayout>
</layout>
这是我的Gradle:APP
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
dataBinding{
enabled=true
}
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.pedrovs.databindingexample"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
在Gradle中也启用了数据绑定。
谢谢!
答案 0 :(得分:0)
使用此xml:
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="person"
type="com.pedrovs.databindingexample.Person" />
</data>
<LinearLayout 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"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{person.name}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{person.lastName}">
</TextView>
</LinearLayout>
</layout>
您在使用xml中的数据绑定时出错,将您的代码与在xml中使用person
的所有地方都碰到了。