gpt4 book ai didi

xml - 如何找出导致 “cannot resolve symbol ' ActivityMainBindingImpl'的原因”

转载 作者:行者123 更新时间:2023-12-03 05:24:02 25 4
gpt4 key购买 nike

我正在关注Udacity android Kotlin Bootcamp,并且在“关于我”应用程序中。

在两次拆分后重建项目后,我已经检查了另一个答案中提到的生成窗口,以查看原始编译器输出,但这只是说与ActivityMainBindingImpl的导入行标题中的内容相同。

我已经尝试过多次重建项目,并使缓存无效并重新启动多次,但是所有这些都没有任何进展。

这与本文https://www.reddit.com/r/Kotlin/comments/gq6r2e/need_help_with_this_confusion_in_data_binding/中提到的问题完全相同

但是,我已经在我的activity_main.xml中省略了@,并且它不能解决问题,并且仍然在两个对话框上都说“找不到标识符'MyName'”

android:text="@{MyName.name}"
android:text="@{MyName.nickname}"

此错误使我无法在模拟器上运行该应用程序,而且我还拥有最新版本的SDK构建,命令行,平台工具和模拟器。在此之前,此应用在我使用的Google Pixel 3模拟器上运行良好

这是我的应用Gradle构建
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"

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 {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

activity_main.xml ,在这两者上我都遇到“找不到标识符'MyName'”错误
android:text="@{MyName.name}"

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

<data>
<variable name="myName"
type="com.example.aboutme.MyName"
/>
</data>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/layout_margin"
android:orientation="vertical"
android:paddingTop="@dimen/small_padding">

<TextView
android:id="@+id/name_text"
style="@style/NameStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:text="@{MyName.name}"
android:textAlignment="center" />

<EditText
android:id="@+id/nickname_edit"
style="@style/NameStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/what_is_your_nickname"
android:inputType="text" />

<Button
android:id="@+id/done_button"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:fontFamily="@font/roboto"
android:text="@string/done" />

<TextView
android:id="@+id/nickname_text"
style="@style/NameStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="@{MyName.nickname}"/>

<ImageView
android:id="@+id/star_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/layout_margin"
android:contentDescription="@string/yellow_star"
app:srcCompat="@android:drawable/btn_star_big_on" />

<ScrollView
android:id="@+id/bio_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
android:id="@+id/bio_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:lineSpacingMultiplier="1.2"
android:paddingStart="@dimen/padding"
android:paddingTop="@dimen/padding"
android:paddingEnd="@dimen/padding"
android:text="@string/bio"
tools:text="@string/bio" />
</ScrollView>


</LinearLayout>

</layout>

和我的MainActivity.kt
import com.example.aboutme.databinding.ActivityMainBinding
import androidx.databinding.DataBindingUtil

class MainActivity : AppCompatActivity() {

private lateinit var binding:ActivityMainBinding

private val myName = MyName("Azhar Sharif")


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this,R.layout.activity_main)

binding.myName = myName
binding.doneButton.setOnClickListener{
addNickname(it)
}
}

private fun addNickname(view:View){

binding.apply {
myName?.nickname = nicknameEdit.text.toString()
nicknameText.text = nicknameEdit.text
invalidateAll()
nicknameEdit.visibility = View.GONE
doneButton.visibility = View.GONE
nicknameText.visibility = View.VISIBLE
}

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

}
}

最佳答案

好吧,我想您没有标识符“MyName”,但是有一个名为“myName”的...

改变这个

android:text="@{MyName.name}"
android:text="@{MyName.nickname}"

对此
android:text="@{myName.name}"
android:text="@{myName.nickname}"

关于xml - 如何找出导致 “cannot resolve symbol ' ActivityMainBindingImpl'的原因”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62346560/

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