我正在尝试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]]
项目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"
}
}
应用程序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
嗨,我认为您需要使用 @Layout/nav_header而不是以下行。
app:headerLayout="@drawable/nav_header"
删除此行:
vectorDrawables.useSupportLibrary = true
添加导航视图的androidx依赖关系
implementation 'com.google.android.material.navigation.NavigationView'