2

我正在尝试使用带有底部导航视图的 androidx 导航。因此,当我将带有 android:name = "androidx.navigation.fragment.NavHostFragment" 的片段放入 xml 文件中时,它会给我一个错误。我猜那个 xml 没有看到这个库或者这个 Fragment

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 

com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)

4

2 回答 2

14

错误在@navigation/mobile_navigation. 您必须app:startDestination<navigation>.

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>
于 2019-06-26T14:05:53.470 回答
2

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"

}

对于科特林:-

dependencies {
def nav_version = "2.3.0"

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

}

参考链接

于 2021-02-02T17:28:49.450 回答