求各位大佬帮忙看一下BindingAdapter遇到的问题,这要怎么解决啊

布局代码:

<?xml version="1.0" encoding="utf-8"?>
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"
tools:context="com.example.dell.mvvmdemo.MainActivity">


name="user"
type="com.example.dell.mvvmdemo.User"/>

android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">

   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:onClick="@{user.onClickName}"
       android:text="@{user.name}"
       android:textSize="20sp"
       app:newName ="@{user.newName}"
       />


javaBean代码:

public class User {
public ObservableField name = new ObservableField<>();

public ObservableField newName = new ObservableField<>();

public void onClickName(View view) {
Log.i("User", name.get());
}

@BindingAdapter({"newName"})
public static void setNewName(TextView tv, ObservableField oName) {
tv.setText(oName.get());
}
}

数据绑定:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);

   ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
   final User user = new User();
   user.name.set("zhangsan");
   binding.setUser(user);

   new Thread(new Runnable() {
       @Override
       public void run() {
           SystemClock.sleep(2000);
           user.name.set("lisi");
           user.newName.set("wangwu");
       }
   }).start();

}
}

求问为什么会报错:

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'app:newName' with parameter type java.lang.String on android.widget.TextView.
file:F:\Android\MVVMDemo\app\src\main\res\layout\activity_main.xml
loc:24:28 - 24:39
****\ data binding error ****

1个回答

根部局是layout吗,或者把build东西删除重新编译一下看看

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!