Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Kotlin数据绑定不起作用,我不知道为什么_Android_Android Studio_Kotlin - Fatal编程技术网

Android Kotlin数据绑定不起作用,我不知道为什么

Android Kotlin数据绑定不起作用,我不知道为什么,android,android-studio,kotlin,Android,Android Studio,Kotlin,我是Android开发新手,一直在网上学习Android开发课程。上一课教我如何使用碎片。我能够使用提供的框架项目文件跟随课程。然而,当我试图在自己的项目中实现这些原则来实践数据绑定时,这些原则是行不通的。有两个错误。最后一次导入给出错误:“未解析引用:数据绑定”,然后另一个错误是“未解析引用:FragmentLoginBinding”。在过去的5个小时里,我一直在浏览论坛帖子和文档,但我找不到我的错误。任何帮助都将不胜感激,谢谢 *编辑:在我生成的java项目文件中没有数据绑定文件 我的代码如

我是Android开发新手,一直在网上学习Android开发课程。上一课教我如何使用碎片。我能够使用提供的框架项目文件跟随课程。然而,当我试图在自己的项目中实现这些原则来实践数据绑定时,这些原则是行不通的。有两个错误。最后一次导入给出错误:“未解析引用:数据绑定”,然后另一个错误是“未解析引用:FragmentLoginBinding”。在过去的5个小时里,我一直在浏览论坛帖子和文档,但我找不到我的错误。任何帮助都将不胜感激,谢谢

*编辑:在我生成的java项目文件中没有数据绑定文件

我的代码如下: fragment\u 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 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>
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\u login.xml
封装起来才能进行数据绑定

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