gpt4 book ai didi

kotlin - 如何修复 kotlin 中的数据绑定(bind)错误?

转载 作者:行者123 更新时间:2023-12-02 10:57:39 24 4
gpt4 key购买 nike

我有一个在线 Kotlin 类(class)。当我尝试应用数据绑定(bind)部分时,我在 .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-在这行代码中:
私有(private)lateinit var绑定(bind):ActivityMainBinding:

(ActivityMainBinding) 为红色字体,错误为( Unresolved reference AcitivityMainBinding)

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

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

最佳答案

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

为此,请转至 Build -> Clean Project然后,Build -> Rebuild Project
希望能成功!

Here's the link如果你想检查另一个问题

关于kotlin - 如何修复 kotlin 中的数据绑定(bind)错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57006964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com