在哪个库中声明了“android.view.vector”?

问题描述 投票:0回答:2

我正在尝试Android Jetpack导航,我收到此错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.navigation/com.example.android.navigation.MainActivity}: android.view.InflateException: Binary XML file line #39: Binary XML file line #39: Error inflating class com.google.android.material.navigation.NavigationView
Caused by: android.view.InflateException: Binary XML file line #39: Binary XML file line #39: Error inflating class com.google.android.material.navigation.NavigationView
Caused by: android.view.InflateException: Binary XML file line #39: Error inflating class com.google.android.material.navigation.NavigationView

Caused by: android.view.InflateException: Binary XML file line #17: Binary XML file line #17: Error inflating class vector
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class vector
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.vector" on path: DexPathList[[zip file "/data/app/com.example.android.navigation-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.android.navigation-1/lib/x86_64, /system/lib64, /vendor/lib64]]

Project build.gradle文件具有以下版本:

buildscript {
    ext {
        kotlin_version = '1.3.0'
        archLifecycleVersion = '1.1.1'
        gradleVersion = '3.3.1'
        supportlibVersion = '1.0.0'
        navigationVersion = '1.0.0-rc02'
        dataBindingCompilerVersion = gradleVersion
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$gradleVersion"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:$navigationVersion"
    }
}

app build.gradle文件具有以下依赖项:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'androidx.navigation.safeargs'

android {
    compileSdkVersion 28
    dataBinding {
        enabled = true
    }
    defaultConfig {
        applicationId 'com.example.android.navigation'
        minSdkVersion 21
        targetSdkVersion 28
        vectorDrawables.useSupportLibrary = true
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    implementation "androidx.appcompat:appcompat:$supportlibVersion"
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha1'

    implementation 'androidx.legacy:legacy-support-v4:1.0.0'    
    implementation "com.google.android.material:material:$supportlibVersion"
    implementation "android.arch.navigation:navigation-fragment-ktx:$navigationVersion"
    implementation "android.arch.navigation:navigation-ui-ktx:$navigationVersion"    
}

谷歌搜索没有产生任何声明“android.view.vector”的库。有谁知道需要添加哪些依赖项?在Android jetpack中遇到过类似的问题?

谢谢。

更新:

activity_main.xml中

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <fragment android:id="@+id/navHostFragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:navGraph="@navigation/navigation"
                app:defaultNavHost="true"/>

        </LinearLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/navView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@drawable/nav_header"
            app:menu="@menu/navdrawer_menu"/>

    </androidx.drawerlayout.widget.DrawerLayout>
</layout>

在layout / nav_header.xml中

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navHeader"
    android:layout_width="match_parent"
    android:layout_height="192dp"
    android:background="?attr/colorPrimaryDark"
    android:padding="16dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/navHeaderImage"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/horizontal_margin"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="@dimen/horizontal_margin"
        android:layout_marginBottom="24dp"
        android:scaleType="fitCenter"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/nav_header" />
</androidx.constraintlayout.widget.ConstraintLayout>

在gradle.properties文件中:

android.enableJetifier=true
android.useAndroidX=true
org.gradle.jvmargs=-Xmx1536m
android navigation android-jetpack
2个回答
0
投票

删除此行:

vectorDrawables.useSupportLibrary = true

0
投票

为导航视图添加androidx依赖项

 implementation 'com.google.android.material.navigation.NavigationView' 

0
投票

嗨,我认为您需要使用@ layout / nav_header而不是以下行。

app:headerLayout="@drawable/nav_header"
© www.soinside.com 2019 - 2024. All rights reserved.