无法从xml文件访问androidx.navigation.fragment.NavHostFragment

j13ufse2  于 2023-01-03  发布在  Android
关注(0)|答案(2)|浏览(176)

我正在尝试使用底部导航视图的androidx导航。所以当我在xml文件中放入带有androidx:name =“androidx.navigation.fragment.NavHostFragment”的片段时,它给了我一个错误。我猜xml看不到此库或此片段

Here is xml file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".weather.WeatherActivity"
        android:orientation="vertical"
        android:weightSum="10"
>
    <androidx.appcompat.widget.Toolbar
            android:id="@+id/w_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            android:theme="@style/ToolbarColoredBackArrow"
    />
        <fragment
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="9.6"
                android:id="@+id/nav_host_fragment"
                app:defaultNavHost = "true"
                app:navGraph = "@navigation/mobile_navigation"
                android:name="androidx.navigation.fragment.NavHostFragment"
                  />
        <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottom_nav"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.4"
                app:menu="@menu/bottom_nav"
        >

        </com.google.android.material.bottomnavigation.BottomNavigationView>

</LinearLayout>

Activity class:
class WeatherActivity : AppCompatActivity() {
    private lateinit var navController: NavController
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(com.example.upgrade.R.layout.activity_weather)
        val toolbar: Toolbar = findViewById(com.example.upgrade.R.id.w_toolbar)
        setSupportActionBar(toolbar)

        navController = Navigation.findNavController(this, com.example.upgrade.R.id.nav_host_fragment)
        bottom_nav.setupWithNavController(navController)
        NavigationUI.setupActionBarWithNavController(this,navController)

    }

    override fun onSupportNavigateUp(): Boolean {
        return NavigationUI.navigateUp(null,navController)
    }
}

导航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"
            android:id="@+id/mobile_navigation"
            xmlns:tools="http://schemas.android.com/tools">
    <fragment
            android:id="@+id/tab1"
            android:name="com.example.upgrade.weather.current_weather.CurrentWeatherFragment"
            android:label="Today's weather"
            tools:layout="@layout/current_weather_fragment">
    </fragment>


    <fragment
            android:id="@+id/tab2"
            android:name="com.example.upgrade.weather.future_weather.list.FutureWeatherFragment"
            android:label="Forecast For Seven Days"
            tools:layout="@layout/future_weather_fragment"/>

    <fragment
            android:id="@+id/tab3"
            android:name="com.example.upgrade.weather.settings.WeatherSettings"
            android:label="Settings"
            tools:layout="@layout/weather_settings_fragment"/>
</navigation>

so it gives an error like this:
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.upgrade, PID: 19046
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.upgrade/com.example.upgrade.weather.WeatherActivity}: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2747)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:165)
        at android.app.ActivityThread.main(ActivityThread.java:6375)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
     Caused by: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class fragment
     Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class fragment
     Caused by: java.lang.IllegalStateException: no start destination defined via app:startDestination for com.example.upgrade:id/mobile_navigation
        at androidx.navigation.NavGraphNavigator.navigate(NavGraphNavigator.java:59)
        at androidx.navigation.NavGraphNavigator.navigate(NavGraphNavigator.java:28)
        at androidx.navigation.NavDestination.navigate(NavDestination.java:341)
        at androidx.navigation.NavController.onGraphCreated(NavController.java:412)
        at androidx.navigation.NavController.setGraph(NavController.java:376)
        at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:216)
        at androidx.fragment.app.Fragment.performCreate(Fragment.java:2414)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1418)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1684)
        at androidx.fragment.app.FragmentManagerImpl.addFragment(FragmentManager.java:1930)
        at androidx.fragment.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3745)
        at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:120)
        at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:405)
        at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:387)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:802)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:752)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:883)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:522)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:430)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.upgrade.weather.WeatherActivity.onCreate(WeatherActivity.kt:22)
        at android.app.Activity.performCreate(Activity.java:6845)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2700)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:165)
        at android.app.ActivityThread.main(ActivityThread.java:6375)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
        at

安卓系统内部操作系统ZygoteInit主页(ZygoteInit.java:802)

bsxbgnwa

bsxbgnwa1#

错误在@navigation/mobile_navigation中。您必须在<navigation>上将片段定义为app:startDestination

Caused by: java.lang.IllegalStateException: no start destination defined via app:startDestination for com.example.upgrade:id/mobile_navigation
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mobile_navigation"
    app:startDestination="@id/rootFragment">

    <fragment
        android:id="@+id/rootFragment"
        .../>
</navigation>
ovfsdjhp

ovfsdjhp2#

**NavHostFragment**无法访问?

android:name="androidx.navigation.fragment.NavHostFragment"

build.gradle中,只需添加以下两个依赖项-
对于Java:-

dependencies {
def nav_version = "2.3.0"

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

}

对于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

相关问题