问题
I am trying to use androidx navigation with bottom navigation view.So when i put fragment with android:name = "androidx.navigation.fragment.NavHostFragment" in xml file it gives me an error. I guess that xml doesn't see this library or this 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)
}
}
Navigation xml file:
<?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)
回答1:
The error is in @navigation/mobile_navigation
. You have to define a fragment as app:startDestination
on your <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>
来源:https://stackoverflow.com/questions/56774596/androidx-navigation-fragment-navhostfragment-is-not-accessible-from-xml-file