Reputation: 15
Why is the function setVm(user) error? This is MainActivity.java:
package com.example.heriprastio.databindingandroid;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.heriprastio.databindingandroid.databinding.ActivityMainBinding;
import com.example.heriprastio.databindingandroid.model.User;
public class MainActivity extends AppCompatActivity {
private User user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
user = new User();
user.setName("Heri");
user.setEmail("[email protected]");
binding.setVm(user);
}
}
This is my model class:
package com.example.heriprastio.databindingandroid.model;
public class User {
String name;
String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
And this is my "activity_main" file layout:
<?xml version="1.0" encoding="utf-8"?>
<layout 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:layout_height="match_parent"
tools:context=".MainActivity">
<data>
<variable
name="vm"
type="com.example.heriprastio.databindingandroid.MainActivity"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{vm.text}"
android:textSize="16sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{vm.email}"/>
</LinearLayout>
</layout>
In the function setVm(user) cannot be applied, why is this? Can you explain, about this error?
And I have to solve this error or replace it with what?
Upvotes: 0
Views: 208
Reputation: 1826
You've declared your activity as variable type in the layout, but in the activity code you're passing an User instance as layout variable. To make it working, you have to make next changes:
In your layout file change
type="com.example.heriprastio.databindingandroid.MainActivity"
to
type="com.example.heriprastio.databindingandroid.model.User"
Moreover, I see that you're using "@{vm.text}"
variable in the layout, even though you don't have such method or field in the User class. I suppose you wanted to use the user's name instead. So you must change it to "@{vm.name}"
Upvotes: 1