如何解决Kotlin中的数据绑定错误?

时间:2019-07-12 12:23:54

标签: kotlin compiler-errors

我正在在线学习Kotlin。当我尝试应用数据绑定部分时,.kt文件中出现一些红色错误。

未导入ActivityMainBinding,因此我手动进行了操作,但问题仍然存在。

这是.kt:

package com.example.aboutme

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import com.example.android.aboutme.databinding.ActivityMainBinding



class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        findViewById<Button>(R.id.Done_button).setOnClickListener { addNickname(it) }
    }
    private fun addNickname(view: View) {
        val editText = findViewById<EditText>(R.id.nickname_editText)
        val nicknameText = findViewById<TextView>(R.id.nickname_text)

        nicknameText.text = editText.text
        editText.visibility = View.GONE
        view.visibility = View.GONE
        nicknameText.visibility = View.VISIBLE

        //Hide the keyboard

        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(view.windowToken, 0)




    }
}

这是.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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_height="match_parent"
        android:layout_width="match_parent">

         <LinearLayout     android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent" android:paddingStart="@dimen/padding"
              android:paddingEnd="@dimen/padding">

    <TextView
            android:text="@string/hanan_tabak"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" android:id="@+id/name_text"
            android:textAlignment="center" style="@style/name_style"/>
    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/nickname_editText" style="@style/name_style" android:hint="@string/what_is_your_nickname"/>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/nickname_text" style="@style/name_style"
            android:layout_gravity="center_horizontal" android:visibility="gone"/>
    <Button
            android:text="@string/done"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/Done_button"
            android:layout_gravity="center_horizontal"
            style="@style/Widget.AppCompat.Button.Colored"/>
    <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" app:srcCompat="@android:drawable/btn_star_big_on"
            android:id="@+id/star_image" android:contentDescription="@string/yellow_star" android:padding="8dp"/>
    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <TextView
                android:text="@string/bio"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" android:id="@+id/scroll_text" style="@style/name_style"
        android:lineSpacingMultiplier="1.2"/>
    </ScrollView>
         </LinearLayout>
</Layout>

这是gradle.app:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.0"
    defaultConfig {
        applicationId "com.example.aboutme"
        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'
        }
    }
    dataBinding { enabled = true}
}

dependencies {
    kapt "com.android.databinding:compiler:3.4.1"

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

这是gradle.project:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.3.31'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我遇到的错误是: 1-在代码行中:

导入com.example.android.aboutme.databinding.ActivityMainBinding:

(android)为红色字体,错误为(Unresolved reference:android)

2-在这行代码中: 私有lateinit var绑定:ActivityMainBinding:

(ActivityMainBinding)为红色字体,错误为(未解析的参考AcitivityMainBinding)

3-在这行代码中: binding = DataBindingUtil.setContentView(this,R.layout.activity_main):

setContentView下有一个红色下划线,并且错误显示(没有足够的信息来推断变量T)

3 个答案:

答案 0 :(得分:0)

我遇到了同样的错误,结果发现这是我输入的错字。 但我可以看到,您的问题并非如此。 尽管当我在寻找答案时,我遇到的问题与其他人提出的问题相同。他显然是通过清理和重建项目来解决的。

要执行此操作,请转到Build -> Clean Project,然后转到Build -> Rebuild Project

希望它能解决!

Here's the link到另一个问题是否要检查

答案 1 :(得分:0)

我在Android Studio中使用文件菜单中的“使缓存无效并重新启动”选项解决了此问题

答案 2 :(得分:0)

在此包装您的布局

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- YOUR LAYOUT HERE -->
</layout>