【问题标题】:Kotlin- xmlns:app="http://schemas.android.com/apk/res-auto" is unavailableKotlin- xmlns:app="http://schemas.android.com/apk/res-auto" 不可用
【发布时间】:2019-11-10 05:16:35
【问题描述】:

我正在关注kotlin fundamentals code lab,其中说API 21 下的任何内容都会将矢量图像转换为png 图像,并防止我需要将xmlns:app="http://schemas.android.com/apk/res-auto" 添加到activity_main.xml 文件并按Sync Now , 并将vectorDrawables.useSupportLibrary = true 添加到defaultConfig { } 文件中的defaultConfig { }) 文件中。我跟踪了所有内容,但一直收到错误消息:

/Users/Home/IdeaProjects/DiceRoller/app/src/main/res/layout/activity_main.xml:12

错误:找不到属性 android:srcCompat。

错误:链接文件资源失败。

我意识到xmlns:app="http://schemas.android.com/apk/res-auto" 行是灰色的

这是我正在使用的 gradle 版本:

dependencies {
    classpath 'com.android.tools.build:gradle:3.2.0-alpha18

是什么导致了这个问题?

activity_main.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center_vertical"
    tools:context=".MainActivity">

<android.support.v7.widget.AppCompatImageView 
           android:id="@+id/diceImageView"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center_horizontal"
           android:srcCompat="@drawable/empty_dice"
           tools:src="@drawable/dice_1">
</android.support.v7.widget.AppCompatImageView>

构建:等级(:应用程序)

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.diceroller"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.core:core-ktx:1.2.0-beta02'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}

build.gradle:

buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0-alpha18'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

【问题讨论】:

  • 顺便说一句,您提到的问题与您面临的真正问题无关,即在错误的命名空间中定义 srcCompat 属性。 (灰色/灰色的命名空间是因为 Android Studio 识别出该命名空间未使用。)依赖关系也与问题无关。
  • (PS 提到您应该将android:src 替换为app:srcCompat 的步骤甚至在该步骤的下方进一步提到:“更改&lt;ImageView&gt; 元素中的android:src 属性成为app:srcCompat。”)
  • 啊啊啊啊,我不知道。我刚开始学习 Kotlin。我以为这意味着它不可用。现在我知道这意味着它没有被使用,我永远不会忘记它。感谢您的信息:)

标签: android xml kotlin


【解决方案1】:

这是因为srcCompat 属性仅存在于AppCompatImageView 类的app (http://schemas.android.com/apk/res-auto) 命名空间中(有关更多信息,请参阅API documentation),而不是默认的android (@987654328 @) 命名空间。

要解决此问题,只需将android:srcCompatandroid 部分重命名为app:srcCompat

<android.support.v7.widget.AppCompatImageView 
           app:srcCompat="@drawable/empty_dice" />

(PS 考虑自动关闭 XML 标记,这样您就不必编写更多代码来关闭 XML 元素。有关更多信息,请参阅this question

【讨论】:

  • 是的,您是正确的,感谢您的帮助!我会听取你关于按照你建议的方式关闭标签的建议。
猜你喜欢
  • 2019-01-17
  • 2021-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-28
  • 2016-11-04
  • 2013-12-07
相关资源
最近更新 更多