繁体   English   中英

Kotlin- xmlns:app="http://schemas.android.com/apk/res-auto" 不可用

[英]Kotlin- xmlns:app=“http://schemas.android.com/apk/res-auto” is unavailable

我正在关注kotlin 基础代码实验室,其中说 API 21 下的任何内容都会将矢量图像转换为 png 图像并防止我需要添加xmlns:app="http://schemas.android.com/apk/res-auto"activity_main.xml文件并按下Sync Now ,然后将vectorDrawables.useSupportLibrary = true添加到.gradle(:app ) 文件中的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
}

This is because the srcCompat attribute only exists in the app ( http://schemas.android.com/apk/res-auto ) namespace for the AppCompatImageView class (see the API documentation for more info), not the default android ( http://schemas.android.com/apk/res/android )命名空间。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM