我在Android Studio的Android数据绑定方面苦苦挣扎。我在IDE中收到两条错误消息,我不知道如何解决这些错误:Cannot resolve symbol 'BR'
和Cannot resolve symbol @{data.visible ? View.VISIBLE : View.GONE}
。
my.namespace
,包namespace.my.databindingtest
)dataBinding { enabled = true }
添加到应用模块的 build.gradle 文件中(并进行了gradle同步)Data
类,其扩展BaseObservable
并具有属性text
(字符串)和visible
(布尔值)activity_main.xml
:已包含在layout
代码中,已添加data
部分并使用了data.text
和data.visible
属性。MainActivity
class build.gradle (模块:应用):
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "namespace.my.databindingtest"
minSdkVersion 9
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'
}
Data.java (包namespace.my.databindingtest
):
package namespace.my.databindingtest;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
public class Data extends BaseObservable {
private String text;
private boolean visible;
public Data(String text, boolean visible) {
this.text = text;
this.visible = visible;
}
@Bindable
public String getText() {
return this.text;
}
public void setText(String text) {
this.text = text;
notifyPropertyChanged(BR.text);
}
@Bindable
public boolean getVisible() {
return this.visible;
}
public void setVisible(boolean visible) {
this.visible = visible;
notifyPropertyChanged(BR.visible);
}
}
activity_main.xml (res / layout):
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="data" type="namespace.my.databindingtest.Data"/>
<import type="android.view.View"/>
</data>
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="namespace.my.databindingtest.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{data.text}"
android:visibility="@{data.visible ? View.VISIBLE : View.GONE}"/>
</RelativeLayout>
</layout>
MainActivity.java (包namespace.my.databindingtest
):
package namespace.my.databindingtest;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import namespace.my.databindingtest.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
Data data = new Data("Hello world!", true);
binding.setData(data);
}
}
在Data.java
文件中,BR
以红色突出显示,提示为Cannot resolve symbol 'BR'
:
在activity_main.xml
文件中,@{data.visible ? View.VISIBLE : View.GONE}
以红色突出显示,提示为Cannot resolve symbol @{data.visible ? View.VISIBLE : View.GONE}
:
那么我该如何解决这些错误?
答案 0 :(得分:2)
我对BR符号有同样的问题。我解决了#34;它只需重新启动Android Studio:/ (我对Android和IntelliJ有点新意) 在处理应该创建BR符号的@Bindable时,似乎有些事情失败了......
答案 1 :(得分:0)
尝试将RelativeLayout更改为LinearLayout。我也有问题让数据绑定准备好运行,但我想我已经读到了不支持RelativeLayout的地方
答案 2 :(得分:0)
在
class Line {
var start: CGPoint
var end: CGPoint
init(start _start: CGPoint, end _end: CGPoint) {
start = _start
end = _end
}
}
这3个步骤帮助我恢复:
答案 3 :(得分:0)
您可以在MV(ModelView)中执行UI逻辑。我的意思是Data.java文件
public class Data extends BaseObservable {
private String text;
private boolean visible;
public Data(String text, boolean visible) {
this.text = text;
this.visible = visible;
}
@Bindable
public String getText() {
return this.text;
}
public void setText(String text) {
this.text = text;
notifyPropertyChanged(BR.text);
}
@Bindable
public boolean getVisible() {
return this.visible ? View.VISIBLE : View.GONE;
}
public void setVisible(boolean visible) {
this.visible = visible;
notifyPropertyChanged(BR.visible);
}
}
在您的xml文件中,您可以像这样添加
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{data.text}"
android:visibility="@{data.visible}"/>
请不要在视图中执行您的UI逻辑,请在MV中执行。