【问题标题】:Mainactivity.kt is not recognizing the ID in Android studio kotlinMainactivity.kt 无法识别 Android Studio kotlin 中的 ID
【发布时间】:2020-11-22 04:52:17
【问题描述】:

我正在尝试在 kotlin 中运行 Android 应用程序。但 Mainactivity.kt 无法识别 Android studio kotlin 中的 ID。

我已经尝试了很多方法来解决这个错误。 我什至尝试进入构建并单击干净的项目, 然后,重建项目。

但错误不会改变它仍然存在。 我在 MainActivity.kt 中输入了 TextView 的 id,但它仍然显示为红色!

请帮忙解决一下!

【问题讨论】:

  • 您尝试运行该应用程序了吗?请分享您的日志猫
  • 你的activity是否导入了相应的布局文件?像这样:import kotlinx.android.synthetic.main.your_layout_file_name.*
  • @mayank1513 先生,我已经尝试运行该应用程序,它显示未解决的参考。
  • @SoumikBhattacharjee 在将其导入到 Kotlinx 时在 Mainactivity 中显示错误

标签: android xml android-studio android-layout kotlin


【解决方案1】:

kotlin synthetic 不再可用。在 Kotlin 1.4.20-M2 JetBrains 中弃用了 Kotlin Android Extensions 编译器插件。相反,使用findViewByIdViewBinding 获取更多信息检查this 问题也检查this 提交

【讨论】:

  • 请贴出相关来源
【解决方案2】:

我测试了 Kotlin 1.4.1 版的代码,它运行良好。

要解决您的错误,您可以使用val tv:TextView = findViewById(R.id.tv_content) 并在您的代码中使用 tv。

第二个选项是您将 build.gradle 文件中的 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.20" 更改为 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10"

正如@Jimale Abdi 所说-

kotlinx.android.synthetic 不再是推荐的做法。移除 支持明确的 findViewById。

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    你需要使用findViewById()

    在你的类中声明一个tv_content 变量:

    private lateinit var tv_content: TextView
    

    在 onCreate 添加:

    tv_content = findViewById(R.id.tv_content)
    

    现在您可以在 Update 函数中调用:

    tv_content.text = "Text you want to set"
    

    【讨论】:

    • 你能告诉我更新功能在哪里吗?
    【解决方案4】:

    确保将插件 kotlin("android.extensions") 添加到 build.gradle(Module)

    【讨论】:

      【解决方案5】:

      最好的方法是使用 dataBinging。请按照以下步骤操作

      1. 在 Gradle 脚本中添加 数据绑定 >>> build.gradle(Module.yourAppName.app)
              release {
                ........
                ........
               
              }
          }
          //add your code here
          dataBinding{
              enabled  = true
          } 
      
      1. 同步项目

      2. activity_main.xml布局中添加layout>标签

      <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">
        
         <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context=".MainActivity">
          
          <TextView
              android:id="@+id/tv_content"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
               android:textSize= "30sp"/>
      
      </LinearLayout>
      </layout>
      
      1. MainActicity.kt中添加数据绑定代码
      class MainActivity : AppCompatActivity() {
      
          private lateinit var binding: ActivityMainBinding //binding variable
      
          override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
              binding = DataBindingUtil.setContentView(this, R.layout.activity_news) //binding content view
      
      
          }
          fun update(view: View){
              binding.tv_content.text = "your text goes here" //apply the binding to text
            }
      
      }
      

      享受:)

      【讨论】:

        【解决方案6】:

        只需打开您构建的 gradle 应用程序文件。 并将您的插件替换为

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

        【讨论】:

          猜你喜欢
          • 2021-02-16
          • 1970-01-01
          • 1970-01-01
          • 2022-01-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-08
          • 1970-01-01
          相关资源
          最近更新 更多