NavHostFragment not accessible from XML

前端 未结 6 1591
天涯浪人
天涯浪人 2021-02-14 06:08

I wanted to try new Navigation library. After following this guideline I experienced error at runtime:

Caused by: android.view.InflateException: Binary XML file          


        
相关标签:
6条回答
  • 2021-02-14 06:33

    NavHostFragment is not accessible?android:name="androidx.navigation.fragment.NavHostFragment

    So in your build.gradle, just add this two dependencies -

    For Java:-

    dependencies {
    def nav_version = "2.3.0"
    
    implementation "androidx.navigation:navigation-fragment:$nav_version"
    implementation "androidx.navigation:navigation-ui:$nav_version"
    
    }
    

    For Kotlin:-

    dependencies {
    def nav_version = "2.3.0"
    
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    
    }
    

    Reference Link:- https://developer.android.com/jetpack/androidx/releases/navigation

    0 讨论(0)
  • 2021-02-14 06:35

    Yep, as mentioned by Levi Albuquerque, this is a known bug in latest Canary Android Studio release (14). You can vote to this bug, subscribe and provide some useful information here.

    Update:

    Issue will be fixed in Android Gradle plugin 3.2.0-alpha17

    0 讨论(0)
  • 2021-02-14 06:37

    Apparently, see here and here, the use of the Jetifier and Android X is still under refactor, In this google i/o talk they've asked us to wait for Canary 15 which has some bug fixes.

    Try to use the navigation library with the old support library for now.

    Edit: Android Studio 3.2 Canary 15 is available for download, everything's working fine for the navigation library. After you've finished installing clear up the clutter invalidating the cache to see if it goes fine.

    0 讨论(0)
  • 2021-02-14 06:47

    Under the navigation folder, there is an XML file called nav_graph.xml. Open it and it will prompt Gradle sync.

    Project Tree

    0 讨论(0)
  • 2021-02-14 06:49

    Adding dependencies from Android Developers helped me:

    dependencies {
       def nav_version = "2.3.0-rc01"
    
      // Java language implementation
      implementation "androidx.navigation:navigation-fragment:$nav_version"
      implementation "androidx.navigation:navigation-ui:$nav_version"
    
      // Kotlin
      implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
      implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    
      // Dynamic Feature Module Support
      implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
    
      // Testing Navigation
      androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
    }
    
    0 讨论(0)
  • 2021-02-14 06:56

    I also just have this headache as I am following Android Kotlin Fundamental tutorial at https://codelabs.developers.google.com/codelabs/kotlin-android-training-add-navigation/index.html#3 Here is the code that works for me

    <androidx.fragment.app.FragmentContainerView
            android:id="@+id/myNavHostFragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:navGraph="@navigation/navigation"
            app:defaultNavHost="true"/>
    

    Apparently the tutorial has not been updated. Also in the dependency need to update the library ..

    ....    
    implementation "androidx.navigation:navigation-fragment:2.3.0"
    implementation "androidx.navigation:navigation-ui:2.3.0"
    

    I am using Android Studio 4.0.

    0 讨论(0)
提交回复
热议问题