Reputation: 61
I am new to Android development and have been working through an Android development course online. The last lesson taught me how to use fragments. I was able to follow along with the lesson using the provided framework project file. However when I tried to implement the principles in my own project to practice the data binding wouldn't work. There are two errors. The last import gives the error: "unresolved reference: databinding", and then another error is "unresolved reference: FragmentLoginBinding". I have spent the last 5 hours combing through forum posts and documentation but I cannot find my error. Any help would be greatly appreciated thank you.
*Edit: There is no databinding file in my generated java project file
My code is as follows: fragment_login.kt:
package com.example.project2019
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import com.example.project2019.databinding.FragmentLoginBinding
class fragment_login : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding = DataBindingUtil.inflate<FragmentLoginBinding>(inflater,R.layout.fragment_login,container,false)
return binding.root
}
}
fragment_login.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_vertical"
tools:context="com.example.project2019.fragment_login">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_blank_fragment"
android:layout_gravity="center_horizontal"
/>
<Button
android:id="@+id/loginbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Log In"
/>
</LinearLayout>
Build.Gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
dataBinding {
enabled = true
}
buildToolsVersion "29.0.1"
defaultConfig {
applicationId "com.example.project2019"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation "com.google.android.material:material:$version_material"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Upvotes: 1
Views: 888
Reputation: 11
Try adding the following lines to the build.gradle
file
buildFeatures {
viewBinding true
}
dataBinding.enabled=true
Upvotes: 1
Reputation: 15423
You have to enclose your fragment_login.xml
with layout
to work data binding
<?xml version="1.0" encoding="utf-8"?>
<layout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_vertical"
tools:context="com.example.project2019.fragment_login">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_blank_fragment"
android:layout_gravity="center_horizontal" />
<Button
android:id="@+id/loginbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Log In" />
</LinearLayout>
</layout>
Upvotes: 3