Skip to content Skip to sidebar Skip to footer

Cannot Access Activity Binding Android

I have a layout file called activity_suggestions. I am using databinding in it. Hence the file ActivitySuggestionsBinding got generated. The project compiles successfully. But when

Solution 1:

In this demo it is work and try this way.. add only databinding enable code into app level gradle file like below code..

dataBinding {
    enabled = true
}

below code is my app level gradle file ..

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.example.adruser.rafdemo"
    minSdkVersion 23
    targetSdkVersion 27
    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:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:design:27.1.1'
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.google.android.gms:play-services-maps:11.6.0'
implementation 'com.google.android.gms:play-services-location:11.6.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.intuit.sdp:sdp-android:1.0.4'
implementation 'com.github.bumptech.glide:glide:4.7.1'


}
repositories {
mavenCentral()
}

then after in project level gradle.properties file add below line..

android.databinding.enableV2=true

make user_layout.xml file like below code..

<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
    <variable
        name="user"
        type="com.example.adruser.rafdemo.model.User"/>
</data>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text='@{user.name}'/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:id="@+id/ulTvName"
        android:text="@{user.dob}"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:id="@+id/ulTvAge"
        android:text="@{Integer.toString(user.age)}"
        app:layout_constraintTop_toBottomOf="@+id/ulTvName"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:id="@+id/ulTvDob"
        android:text="@{user.dob ?? user.expDob}"
        app:layout_constraintTop_toBottomOf="@+id/ulTvAge"
        />
</android.support.constraint.ConstraintLayout>
</layout>

then after make User.java pojo class like below code.. you can make .kt class pojo also

public class User {
public String name,dob,expDob;
public int age;
public User(){}
public User(String name, String dob,String expDob, int age) {
    this.name = name;
    this.dob = dob;
    this.age = age;
    this.expDob=expDob;
}
public static String display(){
    return "rajesh";
}
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDob() {
    return dob;
}

public void setDob(String dob) {
    this.dob = dob;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

}

then finnaly make DatabindingActivity.kt class for binding ..

class DatabindingActivity :AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    var binding:UserLayoutBinding=DataBindingUtil.setContentView(this,R.layout.user_layout)
    val user = User("karan", null, "25/06/1994", 24)
    binding.user=user
}
}

Solution 2:

Add following in you app build.gradle

kapt "com.android.databinding:compiler:$android_plugin_version"
apply plugin: 'kotlin-kapt' // This one at top where plugin belong to

This will do the trick.

$android_plugin_version is version of com.android.tools.build:gradle in application build.gradle

Also, add this to your module build.gradle

android { /// Existing Code kapt { generateStubs = true } }


Solution 3:

I had similar issue. I was getting error

error: cannot access RepeatPaymentFragmentBinding
class file for RepeatPaymentFragmentBinding not found
Consult the following stack trace for details.
com.sun.tools.javac.code.Symbol$CompletionFailure: class file for my.package.databinding.RepeatPaymentFragmentBinding not found

Spent a couple of days to figure out that the problem is not with GDB but with Dagger2. It turned out, Dagger doesn't support using generated classes in code it generates.

So, I had such class in my :library module:

class RepeatPaymentFragment: BaseFragment<RepeatPaymentFragmentBinding>() {
...
}

And the dagger component for it were located in :app module. So the two annotation processors interfere each other.

After I removed generic from my fragments it compiles fine.

So if you have such error, try to look if you try to inject into (or provide) class which is generated by another annotation proccessor, or inherits from such class, or have generic with such class.


Post a Comment for "Cannot Access Activity Binding Android"