我正在尝试在我的 kotlin android 应用程序中绑定(bind)数据,我在 codelab 上关注本教程 Link to Codelab我做的一切都一样。
但是在 MainActitvity.kt
文件中导入 ActivityMainBinding
时,ide 一直以红色文本显示错误。
我确实在此处的应用级 gradle 文件中启用了数据绑定(bind)
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)
}
}
无论如何,我在尝试构建 gradle 时遇到错误。我想试一试。
Compilation Error
我是 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"
工具: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
最佳答案
将命名空间声明移至您的布局标记,并删除重复项。进行更改后应该如下所示:
<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">
...
之后,确保清理并重建您的项目。
关于android - kotlin 应用程序中的 DataBinding 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56310562/