Reputation: 1060
This is the First time i am using Data Binding ,and from Couple of Hours i am having above error. There is Sufficient question related to same Question but Not able to Solve this issue.
Gradle Module APP
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "example.com.test"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
Main_xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="user"
type="example.com.test.MainActivity" />
<import type="android.view.View" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/firstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.firstName}" />
<TextView
android:id="@+id/lastName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.lastName}" />
</LinearLayout>
</layout>
Modal Class
public class User {
public final String firstName;
public final String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
}
Main Activity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String sMan = "Test";
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
User mainActivityPresenter = new User(sMan,sMan);
activityMainBinding.setUser(mainActivityPresenter);
}
}
Image is added of log
I am not able to solve this issue.How can this issue be solved??
Upvotes: 0
Views: 99
Reputation: 1060
Its the Simple and Silly Error i have Done this Time. Solved with Changing the MainActivity to User
<data>
<variable
name="user"
type="example.com.test.User" />
<import type="android.view.View" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/firstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.firstName}" />
<TextView
android:id="@+id/lastName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.lastName}" />
</LinearLayout>
Upvotes: 0
Reputation: 12300
There are lots of things incorrect in your implementation.
BaseObservable
.@Bindable
notifyPropertyChanged(..)
example.com.test.MainActivity
, while I think it is a User
?User mainActivityPresenter = new User(sMan,sMan);
is also very weird. Call a User
"user" and a Presenter
"xxxPresenter". Don't call a User "presenter" if you want to write clear code.... these are some things I see at a glance, there's probably more. Please read the documentation completely first, it will make your implementation a lot easier: https://developer.android.com/topic/libraries/data-binding/index.html
Upvotes: 1