Android in Kotlin: Error in Databinding from the project by Google codelabs

I was doing this course offered by Google, https://codelabs.developers.google.com/codelabs/kotlin-android-training-data-binding-basics/index.html?index=..%2F..android-kotlin-fundamentals#2,

The particular code in github: https://github.com/google-developer-training/android-kotlin-fundamentals-apps/tree/master/AboutMeDataBinding

Here I was doing the data binding, which is shown step by step how to implement it. But at the end I was getting an error with ActivityMainBinding

I have downloaded their solution code from github which on opening with Android-studio 3.4.1 , I got the same problem.

In Main Acctivity.kt

  package com.example.android.aboutme
    import android.content.Context
     import android.os.Bundle
     import android.view.View
   import android.view.inputmethod.InputMethodManager
  import androidx.appcompat.app.AppCompatActivity 
   import androidx.databinding.DataBindingUtil
   import com.example.android.aboutme.databinding.ActivityMainBinding

 class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

private val myName: MyName = MyName("Aleks Haecky")

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = DataBindingUtil.setContentView(this, 
R.layout.activity_main)

    binding.myName = myName

    binding.doneButton.setOnClickListener {
        addNickname(it)
    }

}


 private fun addNickname(view: View) {
    binding. apply {
        myName?.nickname = nicknameEdit.text.toString()
        invalidateAll()
        nicknameEdit.visibility = View.GONE
        doneButton.visibility = View.GONE
        nicknameText.visibility = View.VISIBLE
    }

    // Hide the keyboard.
    val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(view.windowToken, 0)
}
}

Build.gradle(module:app)

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.android.aboutme"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.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 "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    testImplementation 'junit:junit:4.12'
}

I expected to be all okay but ActivityMainBinding was red in colour. Error says unresolved reference.

IDE Fatal Error:
java.lang.NullPointerException
    at com.intellij.build.MultipleBuildsView.lambda$onEvent$2(MultipleBuildsView.java:218)
    at com.intellij.build.MultipleBuildsView.lambda$onEvent$4(MultipleBuildsView.java:310)
    at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.doRun(LaterInvocator.java:435)
    at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.runNextEvent(LaterInvocator.java:419)
    at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.run(LaterInvocator.java:403)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:762)
    at java.awt.EventQueue.access$500(EventQueue.java:98)
    at java.awt.EventQueue$3.run(EventQueue.java:715)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:732)
    at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:755)
    at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:704)
    at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:391)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Unresolved reference: ActivityMainBinding
Unresolved reference: ActivityMainBinding
Unresolved reference: it
Unresolved reference: nicknameEdit
Unresolved reference: invalidateAll
Unresolved reference: nicknameEdit
Unresolved reference: doneButton
Unresolved reference: nicknameText

Upvotes: 1

Views: 1248

Answers (2)

apksherlock
apksherlock

Reputation: 8371

Including the answer of @Chandramauli Chakraborty I also think you might have another error in your code or somewhere in any Kotlin or XML file. PLease make sure you have correctly fixed all of them,and them try to do what the college says.

Upvotes: 0

I got this answer by a trial method. We need to simply, Clean the project followed by Rebuilding. For this to be done, we can simply omit(or delete) the imports which are depicted in grey (i.e. they are unnecessary in the project). Then, Build->Clean Project and after that Build->Rebuild Project.

Upvotes: 1

Related Questions