我目前正在我的应用程序中实现导航组件,但无论我做什么,似乎都无法找到NavHostFragment.

我try 过,重建,失效,重新启动,更改名称,更新我的Android Studio,但似乎没有任何效果.我想这也是为什么我在try 获取MainActivity.kt文件中的NavController时出错的原因,因为它返回null.

Android Studio version 4.0.1

nav_graph.xml image:

enter image description here

nav_graph.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_graph"
    app:startDestination="@id/mainFragment">

    <fragment
        android:id="@+id/mainFragment"
        android:name="com.example.movieapp.ui.main.MainFragment"
        android:label="MainFragment">
        <action
            android:id="@+id/action_mainFragment_to_searchMovieFragment"
            app:destination="@id/searchMovieFragment" />
    </fragment>
    <fragment
        android:id="@+id/searchMovieFragment"
        android:name="com.example.movieapp.ui.search.SearchMovieFragment"
        android:label="SearchMovieFragment" />
</navigation>

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity">

    <include
        layout="@layout/appbar" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

class MainActivity : AppCompatActivity() {

    private lateinit var toolbar: Toolbar
    private val navController by lazy {
        (supportFragmentManager.findFragmentById(R.id.main_fragment) as NavHostFragment).navController
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
        toolbar = findViewById(R.id.mainToolBar)
        setSupportActionBar(toolbar)
        setupActionBarWithNavController(navController)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        val inflater: MenuInflater = menuInflater
        inflater.inflate(R.menu.main_menu_bar, menu)
        return true
    }
}

build.gradle (:app)

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

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        debug {}

        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }

        dataBinding {
            enabled true
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    // Retrofit Libraries
    implementation 'com.squareup.retrofit2:retrofit:2.6.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.6.0'

    // Picasso Library
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    // Navigation libraries
    def nav_version = "2.3.0"
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
}

推荐答案

private val navController by lazy {
        (supportFragmentManager.findFragmentById(R.id.main_fragment) as NavHostFragment).navController
    }

Your navController is null because I think supportFragmentManager.findFragmentById(R.id.main_fragment) returns null. The id of your navHost is R.id.nav_host_fragment according to your activity layout.

Kotlin相关问答推荐

Kotlin:如何将.not()替换为!'# 39;?

Jetpack Compose中的数字 Select 器问题

Kotlin和JavaFX:绑定行为奇怪

为什么onEach不是挂起函数,而Collect是?

解决Microronaut中多个可能的Bean候选者冲突

Microronaut Data 4和JDbi

升级使用jOOQ Gradle插件生成代码失败

init中的NPE抽象函数变量

如何在 Jetpack Compose 中启动和停止动画

如何访问嵌套在另一个 map 中的 map 中的值(在 kotlin 中)

当 func 重载时,kotlin 如何确定调用哪个 func?

如何在 Compose 中创建可重用的修饰符?

在 APK META-INF/library_release.kotlin_module 中复制的重复文件

Spring webflux bean验证不起作用

从列表中的每个对象中 Select 属性

ActivityOptions.makeSceneTransitionAnimation 在具有多个视图的 kotlin 中不起作用

Android:在 DAO 中使用 Room 数据库和 LiveData 架构

Kotlin解构when/if语句

Kotlin中保留的关键字是什么?

在 Kotlin 中创建非绑定服务