我无法在 Android Studio 中按 ID 查看元素
I can't see the elements by their id in Android Studio
我是一名新的 Kotlin 程序员。我试图通过删除 ic_launcher.png 文件并替换我自己的图像来更改应用程序图标。正如我所见,我正在尝试做的事情很荒谬,我复制了“Main Activity.kt”和“main_activity.xml”文件并创建了一个新项目。当我试图将文件复制到新项目中时,程序抛出了很多错误:
没有导入“kotlinx”包,没有感知xml文件中的元素。当我尝试创建另一个新项目以尝试问题是否仅针对该项目时,我意识到 Android Studio 不再感知 xml 元素。
以下是关于我的项目的一些重要信息:
Android清单XML文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ihdovanay.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
项目的构建Gradle文件
buildscript {
ext.kotlin_version = "1.4.10"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
模块的构建Gradle文件
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.ihdovanay.myapplication"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
如果你能解决我的问题,我会很高兴。
在弃用 view synthetics 后修改了答案。
您的 build.gradle
中缺少 kotlin-android-extensions
插件。它负责生成合成视图引用。
kotlin-android-extensions
现已弃用,因此默认情况下不再包含它,不应再将其添加到项目中。您应该使用 findViewById
或 View Binding.
您可以通过执行以下操作使其工作,但我不建议添加和依赖已弃用的库。将行添加到顶部的 plugins
块:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
在 Android Studio 4.1.1 中,build.gradle(模块:yourAppName)
中缺少以下代码
id 'kotlin-android-extensions'
我是一名新的 Kotlin 程序员。我试图通过删除 ic_launcher.png 文件并替换我自己的图像来更改应用程序图标。正如我所见,我正在尝试做的事情很荒谬,我复制了“Main Activity.kt”和“main_activity.xml”文件并创建了一个新项目。当我试图将文件复制到新项目中时,程序抛出了很多错误:
没有导入“kotlinx”包,没有感知xml文件中的元素。当我尝试创建另一个新项目以尝试问题是否仅针对该项目时,我意识到 Android Studio 不再感知 xml 元素。
以下是关于我的项目的一些重要信息:
Android清单XML文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ihdovanay.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
项目的构建Gradle文件
buildscript {
ext.kotlin_version = "1.4.10"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
模块的构建Gradle文件
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.ihdovanay.myapplication"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
如果你能解决我的问题,我会很高兴。
在弃用 view synthetics 后修改了答案。
您的 build.gradle
中缺少 kotlin-android-extensions
插件。它负责生成合成视图引用。
kotlin-android-extensions
现已弃用,因此默认情况下不再包含它,不应再将其添加到项目中。您应该使用 findViewById
或 View Binding.
您可以通过执行以下操作使其工作,但我不建议添加和依赖已弃用的库。将行添加到顶部的 plugins
块:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
在 Android Studio 4.1.1 中,build.gradle(模块:yourAppName)
中缺少以下代码id 'kotlin-android-extensions'