I'm binding data between android fragment and ViewModel in kotlin. Compiler throws
error: cannot find symbolimport >frydzej.yerbet.databinding.FragmentCollectionBindingImpl
I have tried to change gradle version to 3.2.1 and databinding compiler version to 3.2.1. But with same result. My Android Studio version is 3.3 RC 3
This is my project gradle buildscript
buildscript {
ext.kotlin_version = '1.3.11'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0-rc03'
classpath "org.jetbrains.kotlin:kotlin-gradle-
plugin:$kotlin_version"
classpath 'android.arch.navigation:navigation-safe-args-gradle-
plugin:1.0.0-alpha09'
}
}
and app gradle
dataBinding{
enabled = true
}
dependencies {
def lifecycle_version = "2.0.0"
def room_version = "2.0.0"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
implementation 'com.google.android.material:material:1.1.0-alpha02'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha09"
implementation "android.arch.navigation:navigation-ui-ktx:1.0.0-alpha09"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation 'com.cuneytayyildiz:onboarder:1.0.3'
implementation 'com.android.support:design:28.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
testImplementation 'junit:junit:4.12'
debugImplementation 'com.amitshekhar.android:debug-db:1.0.4'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
fragment_collection.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="viewModel" type="frydzej.yerbet.viewModels.CollectionViewModel"/>
</data>
<FrameLayout
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".Views.CollectionFragment">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_item_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="@drawable/ic_add"
android:onClick="@{() -> viewModel.addFabClicked()}"
android:layout_margin="16dp"
app:fabSize="normal"/>
</FrameLayout>
</layout>
and the Fragment view code:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val binding: FragmentCollectionBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_collection, container, false)
val view: View = binding.root
val viewModel: CollectionViewModel = ViewModelProviders.of(this).get(CollectionViewModel::class.java)
binding.viewModel = viewModel
return view
}
In MainActivity databinding there is either
ActivityMainBinding and ActivityMainBindingImpl,
but in CollectionFragment only
FragmentCollectionBinding
is created
I had the same issue:
The solution eventually was that I misspelled one of the values in my fragments XML file.
Take a look at the android:onclick parameter with an additional () I mistakenly added
WRONG
<Button
android:id="@+id/end_game_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@string/end_game"
android:theme="@style/SkipButton"
android:onClick="@{() -> gameViewModel.onGameFinished()()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline" />
RIGHT
<Button
android:id="@+id/end_game_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@string/end_game"
android:theme="@style/SkipButton"
android:onClick="@{() -> gameViewModel.onGameFinished()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline" />
I will advise you check through your fragment's XML file for any typo or misspelled variable names and correct them. This solved my issue. These errors only get caught at compile-time and points to the generated databinding files, making it hard to debug.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With