简体   繁体   中英

Fragments are not displaying in Android Navigation component

Am new with this architecture and am trying to add multiple fragments in the Android Navigation component but the Fragments are not visible in the list.

在此处输入图片说明

As you can see, The host is the only one available.

I also had a look at this question , but it didn't help me out.

Now, these are the dependencies am using from the documentation :

def nav_version = "2.1.0-beta02"
    def nav_version_ktx = "2.1.0-beta02"

     // Java
    implementation "androidx.navigation:navigation-fragment:$nav_version"
    implementation "androidx.navigation:navigation-ui:$nav_version"

    // Kotlin
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version_ktx"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version_ktx"

and this :

buildscript {
    ext.kotlin_version = '1.3.31'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

Then my fragment is as follows :

class BlankFragment2 : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank_fragment2, container, false)
    }


}

and it's xml :

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context=".BlankFragment2">

    <!-- TODO: Update blank fragment layout -->
    <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/hello_blank_fragment"/>

</FrameLayout>

The version of my android studio is 3.4.2

What could be the reason to this ?

I am using the version 2.1.0-alpha02 and have no problems. Try to change your nav_version_ktx and nav_version

Also this a sample on my github , just in case you want to check something. But i think it's a problem of version

好吧,我得到了解决方案,我只是将我的应用程序迁移到使用Androidx ,这使图表按预期工作。

In your nav graph add "tools:layout="@layout/fragment_your_layout"

<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/main_navigation"
    app:startDestination="@id/home2Fragment">

    <fragment
        android:id="@+id/newProfileContentFragment"
        android:name="com.adidas.app.profile2.NewProfileContentFragment"
        android:label="NewProfileContentFragment"
        tools:layout="@layout/fragment_your_layout"
        />

</navigation>

check if you have NavigationComponent dependency in your build.gradle.

 ext.navigation_version = "2.3.5"

 // Navigation
    implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
    implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"
    implementation "androidx.navigation:navigation-dynamic-features-fragment:$navigation_version"
    androidTestImplementation "androidx.navigation:navigation-testing:$navigation_version"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM