【问题标题】:Can not reference any ID from layout in Android Studio无法从 Android Studio 中的布局中引用任何 ID
【发布时间】:2021-04-03 09:37:34
【问题描述】:

我刚开始 Android 开发并遇到了麻烦,安装了最新的稳定版本的 Android Studio,使用 Kotlin,我只是一辈子都无法让 MainActivity 从布局中识别任何 ID。我已经重新启动了项目,清理项目,重建项目,你的名字。如果有人可以提供帮助,请在下面提供代码。

它实际上只是一个 HelloWorld 应用程序,但布局中的任何内容都不能被引用,我再次启动项目,仍然没有任何内容可以引用。

MainActivity 代码

package com.example.findmyage

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

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        

    }
}

对于布局:

<?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:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Find My Age"
        android:textSize="19sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.144" />

    <EditText
        android:id="@+id/yearBornInput"
        android:layout_width="304dp"
        android:layout_height="48dp"
        android:autofillHints=""
        android:ems="10"
        android:hint="Enter Year Born"
        android:inputType="number"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.05" />

    <TextView
        android:id="@+id/ageResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="79dp"
        android:text="Your Age Will Be Here"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/yearBornInput"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/buttonCalc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get My Age"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ageResult"
        app:layout_constraintVertical_bias="0.267" />

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • 您能否添加您尝试引用视图但没有成功的方式?
  • 这能回答你的问题吗? stackoverflow.com/questions/34169562/…
  • 所以我现在尝试了,得到错误 Gradle sync failed: Cannot add task 'clean' as a task with the name already exists.查阅 IDE 日志以获取更多详细信息(帮助 | 显示日志)(373 毫秒),之后我删除了该行并且 gradle 运行完美,尽管我收到一条错误消息,指出我的 Android 许可证不被接受
  • 但是现在我可以在布局中引用 ID,虽然关于 Android 许可证不被接受的警告让我想知道,肯定带有 Kotlin 的 Android Studio 不会有这么多错误吧?
  • 现在,当尝试运行应用程序时,它给了我:原因:未指定 compileSdkVersion。请将它添加到 build.gradle ,但它在 gradle 中,当我尝试运行应用程序时,它也会给我一个 EDIT CONFIGURATIONS 窗口。

标签: android xml kotlin layout


【解决方案1】:

最后,不知何故,尽管使用了最新的 Android Studio,但它需要在 build.gradle(app) 文件中的顶部文件中的 plugins{} 之后的 apply plugin: "kotlin-android-extensions" 命令。

    id "com.android.application"
    id "kotlin-android"
   }

  apply plugin: "kotlin-android-extensions"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 2022-01-03
    • 1970-01-01
    • 2018-11-23
    • 2016-02-13
    • 1970-01-01
    相关资源
    最近更新 更多