【问题标题】:How to figure out what is causing " cannot resolve symbol 'ActivityMainBindingImpl'"如何找出导致“无法解析符号'ActivityMainBindingImpl'”的原因
【发布时间】:2020-10-02 09:44:24
【问题描述】:

我正在关注 Udacity android Kotlin Bootcamp,并且正在使用 About Me 应用程序。

在两个拆分上重建项目以查看原始编译器输出后,我已经检查了另一个答案中提到的构建窗口,但它只是说与 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)

    }
}

【问题讨论】:

  • 清理和重建项目

标签: xml android-studio kotlin gradle data-binding


【解决方案1】:

好吧,我猜你没有标识符“MyName”,而是一个名为“myName”的标识符...

改变这个

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

到这里

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 2014-03-02
    • 2022-08-03
    • 2014-01-17
    • 2013-05-07
    相关资源
    最近更新 更多