android - Kotlin 数据绑定不起作用,我不知道为什么
问题描述
我是 Android 开发的新手,并且一直在学习在线 Android 开发课程。上一课教我如何使用片段。我能够使用提供的框架项目文件来学习课程。但是,当我尝试在自己的项目中实施这些原则来练习数据绑定时,它就不起作用了。有两个错误。最后一个导入给出了错误:“未解析的引用:数据绑定”,然后另一个错误是“未解析的引用:FragmentLoginBinding”。在过去的 5 个小时里,我一直在梳理论坛帖子和文档,但找不到我的错误。任何帮助将不胜感激,谢谢。
*编辑:我生成的java项目文件中没有数据绑定文件
我的代码如下: 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
}
}
片段登录.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>
构建.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'
}
解决方案
你必须附上你fragment_login.xml
的layout
工作数据绑定
<?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>
推荐阅读
- python - 如何完成梯度下降算法代码?
- java - 从一个函数返回多个元素
- javascript - 如何在springboot控制器中将json数据返回给javascript?
- c# - 尝试使用 ASP.NET 核心 Razor 页面读取 SOAP API
- r - 如何检测用英语编写的印地语字符串的文本语言?
- javascript - 如何删除具有相同名称但在不同时间的新 div
- spring-boot - MapStruct 未检测到构建器中的设置器
- linux - 如何使用bash打开多个屏幕并让它们独立运行
- php - 如何对数组基键中的元素求和
- php - Magento 2 原始 SQL 查询将日期更新为 UTC 时区