Reputation: 3023
I am trying to bind data in my kotlin android app, I was following this tutorial on codelab Link to Codelab I did everything same.
But while importing ActivityMainBinding
in MainActitvity.kt
file ide keeps on showing me error in red text.
I did enabled dataBinding in app level gradle file Here
dataBinding {
enabled = true
}
Here I have imported and initialized dataBinding
import android.content.Context
import android.databinding.DataBindingUtil
import android.hardware.input.InputManager
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
//import kotlinx.android.synthetic.main.activity_main.*
import com.example.aboutme.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//setContentView(R.layout.activity_main)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.done_button.setOnClickListener{
addNickname(it)
}
}
private fun addNickname(view: View) {
binding.apply{
nickname_text.text = binding.nickname_edit.text
invalidateAll()
nickname_edit.visibility = View.GONE
done_button.visibility = View.GONE
nickname_text.visibility = View.VISIBLE
}
//Hide the Keyboard
val inn = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inn.hideSoftInputFromWindow(view.windowToken, 0)
}
}
Error i get while trying to build gradle anyway. I thought to give it a try.
Compilation Error
I am new To Android.
activity_main.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"
tools:context=".MainActivity">
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:text="@string/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
style="@style/name_style"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/btn_star_big_on"
android:id="@+id/imageView2"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/nickname_edit" style="@style/name_style" android:hint="what is your nickname"/>
<Button
android:text="Done"
android:layout_gravity="center_horizontal"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/done_button"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/nickname_text" style="@style/name_style"
android:visibility="gone"/>
<ScrollView
android:id="@+id/bio_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
style="@style/name_style"
android:text="@string/bio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bio_text"
android:lineSpacingMultiplier="1.2"
/>
</ScrollView>
</LinearLayout>
</layout>
error in log
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public val Activity.done_button: Button! defined in kotlinx.android.synthetic.main.activity_main
public val Dialog.done_button: Button! defined in kotlinx.android.synthetic.main.activity_main
public val android.app.Fragment.done_button: Button! defined in kotlinx.android.synthetic.main.activity_main
public val android.support.v4.app.Fragment.done_button: Button! defined in kotlinx.android.synthetic.main.activity_main
Unresolved reference: it
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public val Activity.nickname_edit: EditText! defined in kotlinx.android.synthetic.main.activity_main
public val Dialog.nickname_edit: EditText! defined in kotlinx.android.synthetic.main.activity_main
public val android.app.Fragment.nickname_edit: EditText! defined in kotlinx.android.synthetic.main.activity_main
public val android.support.v4.app.Fragment.nickname_edit: EditText! defined in kotlinx.android.synthetic.main.activity_main
> Task :app:buildInfoGeneratorDebug
Upvotes: 1
Views: 938
Reputation: 166
Move the namespace declarations to your layout tag, and remove the duplicates. It should look like this after the changes have been made:
<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=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
After that, make sure to clean and rebuild your project.
Upvotes: 2
Reputation: 6981
1) You need to add distributionUrl in gradle.wrapper-properties for latest android studio version. Your URL must be
distributionUrl=https://services.gradle.org/distributions/gradle-5.1.1-all.zip
2) data binding dependancy in-app level Gradle file
kapt 'com.android.databinding:compiler:3.1.0'
and also add
apply plugin: 'kotlin-kapt'
3) Add kotlin Gradle plugin project level Gradle dependency.
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.31"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
After completing above all just clean, build and rebuild project.
Upvotes: 0