我尝试了一个简单的数据绑定示例,但它给出了错误。
当我尝试运行此代码时,它会给出"错误:(122,95)错误:无法找到符号变量com"
MainActivity.class
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding activityMainBinding=DataBindingUtil.setContentView(this,R.layout.activity_main);
User user=new User("Android","Dev");
activityMainBinding.setUser(user); }
}
User.class
public class User {
private String name;
private String lastName;
public User(String name, String lastName) {
this.name = name;
this.lastName = lastName;
}
public String getName() {
return name;
}
public String getLastName() {
return lastName;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="root.com.myapplicationbinding.MainActivity">
<data>
<variable
name="user"
type="root.com.myapplicationbinding.User" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/firstnameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.name}" />
<TextView
android:id="@+id/firstnameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.lastName}"
android:layout_toRightOf="@+id/firstnameLabel"/>
</RelativeLayout>
</layout>