【问题标题】:Android Kotlin - viewBindinding not workingAndroid Kotlin - viewBindinding 不起作用
【发布时间】:2021-11-17 23:59:27
【问题描述】:

我遵循这条指令:

https://stackoverflow.com/a/65903308/10642456

来自“Geek Tanmoy”的回答

但我在大多数视图上仍然有unresolved Reference,当在它们之前添加binding. 时,它只是没有做任何事情。

可能是什么问题?

这是一个例子:

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    binding.skipProfileImage.setOnClickListener {
        finish()
    } 
}

Unresolved referenceskipProfileImage

编辑:

这不是ActivityMain,而是另一个,我会分享它,因为它是我重建项目时首先出现错误的地方:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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_width="match_parent"
    android:layout_height="match_parent"
    android:background="#303030"
    tools:context=".AskProfileImage">

    <ImageView
        android:id="@+id/imageView97545"
        android:layout_width="108dp"
        android:layout_height="64dp"
        android:layout_marginTop="8dp"
        android:background="#00FFFFFF"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/logo2" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="32dp"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView97545">

        <TextView
            android:id="@+id/textView22"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/upload_a_profile_image"
            android:textColor="@color/colorAlmostWhite"
            android:textSize="22sp"
            android:textStyle="bold" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/chooseProfileImage"
            android:layout_width="104dp"
            android:layout_height="104dp"
            android:layout_marginTop="48dp">

            <ImageView
                android:id="@+id/imageView10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/ic_circle"
                app:tint="@color/colorWhite" />

            <ImageView
                android:id="@+id/askProfileImageCam"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginStart="28dp"
                android:layout_marginTop="26dp"
                android:layout_marginEnd="28dp"
                android:layout_marginBottom="30dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/ic_camera"
                app:tint="@color/colorText" />
        </androidx.constraintlayout.widget.ConstraintLayout>

        <TextView
            android:id="@+id/chooseProfileImageAlternative"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:padding="8dp"
            android:text="@string/tap_to_choose_an_image"
            android:textColor="@color/colorThemeSecond"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/skipProfileImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="48dp"
            android:padding="8dp"
            android:text="@string/Skip"
            android:textColor="@color/colorTextBitDarker"
            android:textStyle="bold" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • 尝试运行代码,有时编译时错误仍然存​​在,即使没有错误。
  • 你能贴一些代码和logcat吗!惩罚别人无济于事
  • 日志有数千行,这是它随机纠察的一小部分:2021-09-24 20:27:53.611 22223-23567/? D/GOS:GlobalSettingsContainer: setRecommendedMode(), [0, 1, 2, 3] 2021-09-24 20:27:53.611 3664-3834/? D/StorageManagerService:getExternalStorageMountMode:最终 mountMode=1,uid:10025,packageName:com.samsung.android.game.gamehome 2021-09-24 20:27:53.611 22223-23567/? D/GOS:GlobalSettingsContainer: setEnabledFeatureFlag(), src : 342277969731031827, 结果: 54047593575125267
  • 我加了一个例子
  • 你能Build &gt; Make Project看看

标签: android android-viewbinding


【解决方案1】:

绑定是根据您为此活动创建的 XML 布局文件的名称生成的。

我建议仔细检查它是否存在以及它的名称,因为您的代码表明它应该被称为activity_main.xml

【讨论】:

  • 它确实存在,在我删除 kotlin-android-extensions 并添加 buildFeatures { viewBinding true } 后,一切都崩溃了,就像在多个教程中一样。在此之前一切正常。如果我不明白你的意思,请详细解释一下
  • 你能分享activity_main.xml的XML吗?
  • 请检查编辑
【解决方案2】:

添加

<layout> 

在布局的最顶部和

</layout> 

在布局的最底部。

【讨论】:

  • @Eduard Unruh 请试试这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-24
  • 2016-12-26
  • 1970-01-01
  • 1970-01-01
  • 2022-10-12
  • 2023-03-23
  • 2023-03-15
相关资源
最近更新 更多