我正在使用Jetpack Navigation来处理Fragments的导航。在关注文档后,当我运行应用程序时,我收到以下错误。
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.navigation.fragment.NavHostFragment"
的build.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的活动代码
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。
答案 0 :(得分:4)
如果在使用发布版本时发生此问题,则很可能是Proguard缩小了类名。这样,XML文件中的引用android:name="androidx.navigation.fragment.NavHostFragment"
将不匹配任何有效的类。
尝试在proguard文件中添加此行
-keepnames class androidx.navigation.fragment.NavHostFragment
这样就不会使类名变小,并且保持XML文件中提到的那样。
答案 1 :(得分:0)
尝试致电Navigation.findNavController(bottom_navigation_view_homepage_activity, R.id.appointmentFragment)
您也不需要设置setOnNavigationItemSelectedListener
,只需按照以下步骤编辑导航图“ nav_graph”:
<fragment
android:id="@+id/action_appointment"
..../>
<fragment
android:id="@+id/action_chat"
..../>
基本上,您在nav_graph中设置的片段ID与底部导航视图中的项目ID相同。
答案 2 :(得分:0)
在Kotlin中,您不需要调用findviewbyid,请在您的布局中实现数据绑定。
请检查在Bootomnavigaiton中调用的“ id_menu”是否与导航图中的“ id_fragment”相同
您可以使用此方法对bottom_nav进行导航,以显示类似于他的片段
setSupportActionBar(工具栏)
navController = Navigation.findNavController(this, R.id.main_fragment)
NavigationUI.setupWithNavController(bottom_nav,navController)
NavigationUI.setupActionBarWithNavController(this, navController)
也不要忘记覆盖此方法
override fun onSupportNavigateUp(): Boolean {
return NavigationUI.navigateUp(navController,null)
}
请告诉我这段代码是否有效。
答案 3 :(得分:0)
您是否添加了片段依赖项? 实现“androidx.fragment:fragment:1.2.0”这是针对java的我不知道kotlin是否一样