为什么我得到“未解析的参考"?在 Kotlin 中输入视图名称/ID 时出错? [英] Why do I get "unresolved reference" error for my view's name/ID when I type it in Kotlin?

查看:36
本文介绍了为什么我得到“未解析的参考"?在 Kotlin 中输入视图名称/ID 时出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在完全按照教程进行操作.我在 android:id 下以 XML 格式为视图命名.当我在 Kotlin 中键入该名称时,它以红色突出显示,并且有一个未解析的引用".错误.

I am following a tutorial exactly. I gave the view a name in XML under android:id. When I type that name in Kotlin, it is highlighted in red and there is an "unresolved reference" error.

例如,在 XML activity_main.xml 中:

For example, in XML activity_main.xml:

<TextView
    android:id="@+id/nameTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

在 Kotlin MainActivity.kt 中:

In Kotlin MainActivity.kt:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    
    nameTextView // <-- This is highlighted red with error so I can't use it!
}

作为 Android 新手,我不知道这是什么,但我尝试使用的功能称为合成属性"、合成视图属性"、Kotlin Android 扩展"或";kotlin-android-extensions",并且该功能已弃用".:)

推荐答案

在 Kotlin 中通过视图的 ID/名称直接引用视图的能力称为合成属性".它是一个名为 Kotlin Android Extensions 的项目插件的功能.

The ability to refer to a view directly by it's ID/name in Kotlin is called "synthetic properties" and it is a feature of a project plugin called Kotlin Android Extensions.

Google 和 JetBrains 决定弃用 Kotlin Android 扩展,这意味着他们不再支持它,并阻止您使用它.自从它被弃用后,当您在 Android Studio 中创建新项目时,该插件不再包含在新项目中.因此,新项目不支持合成属性,您将获得未解析的引用".如果您尝试使用它,则会出错.

Google and JetBrains decided to deprecate Kotlin Android Extensions, meaning they no longer support it, and discourage you from using it. Ever since it was deprecated, when you create a new project in Android Studio, the plugin is no longer included in the new project. Therefore, synthetic properties are not supported in new projects and you will get an "unresolved reference" error if you try to use it.

2017 年到 2020 年间写的教程经常用到这个功能,如果还没有更新,他们可能连名字都没有提到,因为它被认为是新项目中包含的插件是理所当然的.

Tutorials written between 2017 and 2020 often make use of this feature, and if they haven't been updated, they probably don't even mention it by name, because it was taken for granted to be an included plugin in new projects.

Google 在 这篇博文,主要有以下原因:

Google explained the reasons for deprecating it in this blog post, with these key reasons:

  • 它们污染了全局命名空间
  • 它们不公开可空性信息
  • 它们仅适用于 Kotlin 代码

获取视图引用的快速简便的方法是使用 findViewById.视图的类型应该放在括号 <> 内.在 Activity 中,它看起来像这样:

The quick and easy way to get your view reference is to use findViewById. The type of View should go inside the brackets <>. In an Activity, it looks like this:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    
    val nameTextView = findViewById<TextView>(R.id.nameTextView)

    // Now you can refer to the view using the variable
    nameTextView.setText(R.string.hello_world)
}

在 Fragment 中,您可能会在 onViewCreated 函数中使用视图,因此您必须在父视图上调用 findViewById.(如果您需要在 Fragment 的其他地方访问它,请使用 requireView() 而不是 view.

In a Fragment, you would probably be working with the view in the onViewCreated function, so you must call findViewById on the parent view. (If you need to access it elsewhere in the Fragment, use requireView() instead of view.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val nameTextView = view.findViewById<TextView>(R.id.nameTextView)
    //...
}

如果您只想完成在 Kotlin Android 扩展被弃用之前编写的教程,

findViewById 可能是目前最好的选择.

findViewById is probably the best option for now if you just want to complete your tutorial that was written before Kotlin Android Extensions was deprecated.

然而,使用 findViewById 可能很乏味,而且也容易出错,因为如果您正在搜索不在当前布局中的视图,它不会警告您.如果这样做,它将在运行时崩溃.为此,Google 建议使用视图绑定.开始使用视图绑定有几个步骤,但是一旦你设置了它,它是一个比 findViewById 更简洁的选项.官方说明在这里.

However, using findViewById can be tedious, and it is also error prone, because it won't warn you if you are searching for a view that isn't in the current layout. If you do, it will crash at runtime. For this reason, Google recommends using View Binding. There are a few steps to get started with view binding, but once you set it up, it is a cleaner option than findViewById. The official instructions are here.

最后,如果您真的不关心 Kotlin Android Extensions 已被弃用并且无论如何都想使用它,它目前仍然可以正常工作,但是您必须将该插件添加到您的新项目中以启用它.为此,请打开 app 模块的 build.gradle 文件.在 plugins 块的顶部,您可以为 kotlin-android-extensions 添加一行,如下所示:

Finally, if you really don't care that Kotlin Android Extensions is deprecated and want to use it anyway, it currently still works OK, but you have to add the plugin to your new project to enable it. To do that, open the build.gradle file for your app module. At the top in the plugins block, you can add a line for kotlin-android-extensions, like this:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
}

然后按将项目与 Gradle 文件同步";按钮以启用它.

Then press the "Sync project with Gradle files" button in the toolbar to enable it.

这篇关于为什么我得到“未解析的参考"?在 Kotlin 中输入视图名称/ID 时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆