Android 导航组件:未找到 NavHostFragments 错误

efo*_*foc 13 android kotlin android-jetpack android-jetpack-navigation

我目前正在我的应用程序中实现导航组件,但似乎发现 NavHostFragment 无论我做什么都不起作用。

我已经尝试过重建、失效和重新启动、更改名称以及更新我的 Android Studio,但似乎没有任何效果。我认为这也是我尝试在 MainActivity.kt 文件中获取 NavController 时出错的原因,因为它返回 null。

安卓工作室 4.0.1 版

nav_graph.xml 图像

在此处输入图片说明

导航图.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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

主活动.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
    }
}
Run Code Online (Sandbox Code Playgroud)

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"
}
Run Code Online (Sandbox Code Playgroud)

Len*_*nin 13

我是能够解决问题的方式是通过进入Activity.xml和改变片段类/组件标签androidx.fragment.app.FragmentContainerViewfragment。然后它出现在导航图的主机面板中。


小智 6

我遇到了同样的问题,我通过从DesignPalette创建 NavHostFragment然后分配相应的导航资源文件来解决这个问题。


Dar*_*shn 5

如Doc中所述,您还可以使用布局编辑器添加 NavHostFragment。

  1. 在项目文件列表中,双击活动的布局 XML 文件以在布局编辑器中将其打开。
  2. 在“调色板”窗格中,选择“容器”类别,或者搜索“NavHostFragment”。
  3. 将 NavHostFragment 视图拖到您的 Activity 上。
  4. 接下来,在出现的“导航图”对话框中,选择与此 NavHostFragment 关联的相应导航图,然后单击“确定”。

此后您可能需要重新启动 ANDROID STUDIO(文档中未提及)

此外,当您尝试使用上述方法添加片段时,您可能会看到“fragment”而不是“FragmentContainerView”。


Dat*_*Tat 2

private val navController by lazy {
        (supportFragmentManager.findFragmentById(R.id.main_fragment) as NavHostFragment).navController
    }
Run Code Online (Sandbox Code Playgroud)

navController是空的,因为我认为supportFragmentManager.findFragmentById(R.id.main_fragment)返回空。navHost 的 idR.id.nav_host_fragment根据您的活动布局。