3

我用 Android studio 4 (4.11) 的 kotlin 制作了 android 应用程序。

findViewById 在 Androd Studio 4 中已弃用,然后我使用 viewBinding。 https://developer.android.com/topic/libraries/view-binding

但是 viewBinding 没有出现错误。

(path)/MainActivity.kt: (6, 31): Unresolved reference: ActivityMainBinding

有人可以告诉我错误或我的错误的原因吗?

代码如下。

构建等级:

  android {
  ...
// <-- added
    buildFeatures {
        viewBinding = true
    }
// -->
  }

资源/布局/activity_main.xml:

<?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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/viewText" // added.
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

private lateinit var binding: ActivityMainBinding // added. <== error (6:31)

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

// <-- added.
        binding = ActivityMainBinding.inflate(layoutInflater)
        val view = binding.root
        binding.textView.text = "Test view binding."
// -->

        setContentView(R.layout.activity_main)
    }
}
4

6 回答 6

10

在构建启用了视图绑定的项目时,我也收到了数据绑定错误。这个问题和一些答案可能对任何遇到北极狐 + Java 11问题的人有用。升级后,我将 app build.gradle 条目中的编译选项更改为:

    sourceCompatibility JavaVersion.VERSION_11 
    targetCompatibility JavaVersion.VERSION_11
//Required if using new Java 11 language features in your project

我收到一个错误:找不到包 (*.databinding),并认为没有生成视图绑定。在尝试了许多补救措施并找到了生成的绑定之后,我遇到了上述问题。由于我没有在我的项目中使用任何新的语言特性,我将 build.gradle 中的编译选项改回:

compileOptions {
     sourceCompatibility JavaVersion.VERSION_1_8
     targetCompatibility JavaVersion.VERSION_1_8
}

重建项目,绑定全部恢复,没有编译错误。

根据teralser 的回答,“这个问题已得到修复并出现在 BB canary 8 中。它特定于 Java 11”。

对于使用 Android Gradle Plugin 3.0.0+ 运行 Android 4.0(API 级别 14)或更高版本的设备,根据链接添加 Java 8 编译选项可能会解决您的问题。

于 2021-08-26T18:16:56.880 回答
7

其中之一可能会有所帮助

  • 重建项目(构建->重建项目)
  • 文件 -> 使 Chaches 无效 -> 无效并重新启动
于 2020-12-08T06:09:14.860 回答
2

这样做:- binding = ActivityMainBinding.inflate(layoutInflater)

    val view = binding.root
    
    setContentView(view)  or setcontentview(binding.root)

    binding.textView.text = "Test view binding."

它应该工作。

于 2021-03-19T06:58:36.997 回答
0

只需更换:

setContentView(R.layout.activity_main)

和:

setContentView(binding.root)

单独传递您的布局将在没有任何视图绑定的情况下膨胀第二个布局。

于 2021-09-14T10:33:58.880 回答
-1

很容易解决。创建您在窗口上应用的布局的副本,并从现在开始使用此复制的布局,解决。

于 2021-03-24T07:31:50.873 回答
-1

使用 setContentView(view) 或 setContentView(binding.root) 更改 setContentView(R.layout.activity_main)

于 2021-06-15T17:00:52.533 回答