我正在使用 Jetpack Navigation 来处理 Fragments 的导航。遵循文档后,当我运行应用程序时,出现以下错误。
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.navigation.fragment.NavHostFragment"
构建.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 'android-P'
defaultConfig {
applicationId "com.meditab.imspatient"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
def nav_version = "1.0.0-alpha01"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.constraintlayout:constraintlayout:1.1.0' // Constraint layout
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" // Kotlin
// Anko
implementation "org.jetbrains.anko:anko-commons:$anko_version" // Anko Commons
implementation "org.jetbrains.anko:anko-design:$anko_version" // For SnackBars
implementation "org.jetbrains.anko:anko-coroutines:$anko_version" // Anko Coroutines
// Support libraries
implementation 'androidx.legacy:legacy-support-v4:1.0.0-alpha1' // Support v4
implementation 'androidx.appcompat:appcompat:1.0.0-alpha1' // AppCompat
implementation 'androidx.recyclerview:recyclerview:1.0.0-alpha1' // RecyclerView
implementation 'com.google.android.material:material:1.0.0-alpha1' // Design Support library
implementation 'androidx.cardview:cardview:1.0.0-alpha1' // CardView
// Jetpack Navigation
implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
implementation "android.arch.navigation:navigation-ui-ktx:$nav_version"
// Dagger 2
implementation 'com.google.dagger:dagger:2.14.1'
kapt 'com.google.dagger:dagger-compiler:2.14.1'
provided 'javax.annotation:jsr250-api:1.0'
// Retrofit, OkHttp, OkHttpInterceptor and GSON
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
implementation 'com.rengwuxian.materialedittext:library:2.1.4' // Material EditText
}
kotlin {
experimental {
coroutines "enable"
}
}
托管 NavHostFragment 的 Activity 的代码
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home_page)
// Handle the clicks for bottom navigation view
bottom_navigation_view_homepage_activity.setOnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.action_appointment -> findNavController(item.itemId).navigate(R.id.appointmentFragment)
R.id.action_chat -> findNavController(item.itemId).navigate(R.id.chatFragment)
}
}
Navigation.findNavController(this, R.id.appointmentFragment)
}
活动的 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent">
<fragment
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation_view_homepage_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:itemIconTint="@drawable/nav_item_color_state"
app:itemTextColor="@drawable/nav_item_color_state"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_navigation_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
我已经正确实现了导航图。此外,该项目被重构为 AndroidX。