首页 > 解决方案 > 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'
}

标签: androidandroid-studiokotlin

解决方案


你必须附上你fragment_login.xmllayout工作数据绑定

<?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>

推荐阅读