Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved Reference : FragmentTitleBinding

I want to use fragments in my android application but I can't import FragmentTitleBinding in my fragment class I tried all possible solutions on the internet

  1. I checked layout tags in xml file
  2. I edit build.gradle file
  3. sync project
import com.example.android.navigation.databinding.FragmentTitleBinding

class TitleFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    val binding : com.example.android.navigation.databinding.FragmentTitleBinding=
            DataBindingUtil.inflate(inflater,R.layout.fragment_title,container,false)
    return binding.root;
    }
}

Fragment XML

 <?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"
    tools:context="com.example.android.navigation.TitleFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/titleConstraint"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/playButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/horizontal_margin"
            android:layout_marginEnd="@dimen/horizontal_margin"
            android:paddingStart="@dimen/button_padding"
            android:paddingEnd="@dimen/button_padding"
            android:text="Play"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/button_text_size"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/titleImage" />

            <ImageView
            android:id="@+id/titleImage"
            android:layout_width="0dp"
            android:layout_height="@dimen/image_header_height"
            android:layout_marginStart="@dimen/horizontal_margin"
            android:layout_marginEnd="@dimen/horizontal_margin"
            android:scaleType="fitCenter"
            app:layout_constraintBottom_toTopOf="@+id/playButton"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/android_trivia" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

build.gradle

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

android {
    compileSdkVersion 28
    dataBinding {
        enabled = true
    }
    defaultConfig {
        applicationId 'com.example.android.navigation'
        minSdkVersion 19
        targetSdkVersion 28
        vectorDrawables.useSupportLibrary = true
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
    buildToolsVersion = '28.0.3'
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    // Kotlin
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$version_kotlin"
    // Constraint Layout
    implementation "androidx.constraintlayout:constraintlayout:$version_constraint_layout"
    // Core
    implementation "androidx.core:core:$version_core"
    // Material Design
    implementation "com.google.android.material:material:$version_material"
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    kapt 'com.android.databinding:compiler:3.3.0'
}
kapt {
    generateStubs = true
}

I want to resolved FragmentTitleBinding but it gives an error unresolved reference

like image 586
AndroidDev Avatar asked Dec 05 '22 09:12

AndroidDev


1 Answers

Since databinding perform like annotation processor, you have to clean your project then rebuild again. If that way not work. Try this: 1. Click file menu 2. Select Invalidate Caches/ Restart

image

I hope it works. And please aware that your databinding class has same name with your layout name

Update

Make sure you have these inside your dependencies

kapt "androidx.lifecycle:lifecycle-compiler:2.0.0"

and

android{
dataBinding {
        enabled = true
    }
}

also you need to check Repository in SDK tools sdk tools

like image 150
Nanda Z Avatar answered Dec 18 '22 10:12

Nanda Z