I am trying to use the new navigation graph in android for swapping between fragments. However, I get an error saying :
This navigation graph is not referenced from any layout files (expected to find it in at least one layout file with a NavHostFragment with app:navGraph="#navigation/nav_graph" attribute). Navigation resource files must be referenced from a NavHostFragment in a layout in order to be relevant.
I have seen many poeple with the same error, but everyone seemed to have done something wrong and could correct it. In my case, I am almost 100% sure my code is ok...
Here is my nav_graph.xml :
<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/nav_graph"
app:startDestination="#id/mainFragment">
<fragment
android:id="#+id/mainFragment"
android:name="com.ouimet.budget.MainFragment"
android:label="fragment_main"
tools:layout="#layout/fragment_main" >
<action
android:id="#+id/action_mainFragment_to_expenseFragment"
app:destination="#id/expenseFragment" />
</fragment>
<fragment
android:id="#+id/expenseFragment"
android:name="com.ouimet.budget.ExpenseFragment"
android:label="fragment_expense"
tools:layout="#layout/fragment_expense" />
</navigation>
Along with my MainActivity's resource file activity_main.xml (which references the nav_graph.xml):
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
And my fragment_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainFragment"
android:id="#+id/parentLayout">
<Button
android:id="#+id/view_expense_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Entrer une dépense"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="parent"
app:layout_constraintRight_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
As we can see, my nav_graph is referenced.. Also, when I compile and run the code, the transition between the fragments works! Is this a known issue or just me not seeing something?
Thank you for your help!
Alexandre
Related
Im using viewbinding, and i get this error when i build
Configurations for activity_main.xml must agree on the root element's ID.
Missing ID:
- layout
#+id/drawer_layout:
- layout-land
It says i need to have two different id for rootview. But i have different root for portrait layout and landscape layout. Like below. If i put same id to drawerlayout(landscape) and same id for constraintlayout(portrait) it creates problem in code. That to i wrote common code without checking orientation.
How to get rid of this error, using <include id> or <merge> or anyother ways?
(i dont know how these works exactly for my needs) without checking orientations in code blocks.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/purple_500"
android:theme="#style/Widget.AppCompat.Toolbar"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="#navigation/mobile_navigation"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#+id/bottom_nav"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/nav_host_fragment"
app:layout_constraintVertical_bias="1.0"
app:menu="#menu/menu_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
land/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?colorPrimary"
android:theme="#style/ToolbarTheme" />
<fragment
android:id="#+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="#navigation/mobile_navigation"
app:defaultNavHost="true" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/menu_navigation" />
</androidx.drawerlayout.widget.DrawerLayout>
code
NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)
You're using a </androidx.constraintlayout.widget.ConstraintLayout> for the portrait and a </androidx.drawerlayout.widget.DrawerLayout> for the landscape orientation. Hence the error.. You can try running the app after wrapping the drawer layout in a constraint layout and giving each an identical id
I'm running into a wall debugging what should be a simple issue: I have an app with one activity, which contains a navigation graph, which should display a fragment on start. But it isn't.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<androidx.fragment.app.FragmentContainerView
android:id="#+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph"/>
</FrameLayout>
res/navigation/nav_graph.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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/nav_graph"
app:startDestination="#id/listFragment"
>
<fragment
android:id="#+id/listFragment"
android:name="org.example.my_app.ui.ListFragment"
android:label="fragment_list"
tools:layout="#layout/fragment_list"
>
<action
android:id="#+id/action_listFragment_to_detailFragment"
app:destination="#id/detailFragment"
/>
</fragment>
<fragment
android:id="#+id/detailFragment"
android:name="org.example.my_app.ui.DetailFragment"
android:label="fragment_detail"
tools:layout="#layout/fragment_detail"
/>
</navigation>
fragment_list.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=".ui.ListFragment"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This should be visible"
/>
</FrameLayout>
ListFragment.kt
class ListFragment : Fragment(R.layout.fragment_list)
Despite this dead-simple setup, I see a blank screen (just an app bar) and debug listeners added to the ListFragment class init don't ever get called. Why isn't my navGraph initializing an instance of the fragment?
Documentation:
NavHostFragment provides an area within your layout for self-contained navigation to occur.
You are missing to declare the fragment placeholder as a NavHostFragment in the FragmentContainerView via the android:name attribute; and therefore the navigation won't occur.
<androidx.fragment.app.FragmentContainerView
....
android:name="androidx.navigation.fragment.NavHostFragment"
I'm using Android Studio 3.4.2 and trying to implement some basic navigation features in my app.
I am following the official documentation to setup a NavHostFragement (via a FragmentContainerView) and linking the appropriate graph to it. However, when I do this, I get an error on the app:navGraph attribute stating:
Unexpected resource type: 'navigation' expected: string.
This causes the app to crash on start-up and the nav_graph itself gives a warning that it's not linked to a host.
Main activity:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar"
app:navGraph="#navigation/nav_graph" />
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="358dp"
android:layout_height="64dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="696dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
nav_graph.xml (It already contains a couple off fragments and actions):
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/nav_graph"
app:startDestination="#id/home_screen">
<fragment
android:id="#+id/home_screen"
android:name="com.example.ordersysteem2.home_screen"
android:label="home_screen" >
<action
android:id="#+id/action_home_screen_to_prepare"
app:destination="#id/prepare" />
<action
android:id="#+id/action_home_screen_to_kalkoenSalami"
app:destination="#id/kalkoenSalami" />
</fragment>
<fragment
android:id="#+id/prepare"
android:name="com.example.ordersysteem2.prepare"
android:label="fragment_prepare"
tools:layout="#layout/fragment_prepare" />
<fragment
android:id="#+id/kalkoenSalami"
android:name="com.example.ordersysteem2.kalkoenSalami"
android:label="fragment_kalkoen_salami"
tools:layout="#layout/fragment_kalkoen_salami" />
</navigation>
I have followed a guide on creating android navigation with the androidx navigation package. However, I am having troubles displaying anything. When the app starts it shows and empty activity and not the content of my fragment.
Do you have to do anything extra in order to let it show the start screen of the navigation graph?
Launcher xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_height="match_parent"
android:layout_width="match_parent">
<fragment
android:id="#+id/launcherNavHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="#navigation/navigation_launcher" />
</FrameLayout>
Navigation graph (navigation_launcher)
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/navigation_launcher"
app:startDestination="#id/launcherFragment">
<fragment
android:id="#+id/launcherFragment"
android:name="com.myapp.Views.LauncherFragment"
android:label="fragment_launcher"
tools:layout="#layout/fragment_launcher" >
<action
android:id="#+id/action_launcherFragment_to_mainActivity"
app:destination="#id/mainActivity" />
</fragment>
<activity
android:id="#+id/mainActivity"
android:name="com.myapp.Views.MainActivity"
android:label="activity_main"
tools:layout="#layout/activity_main" />
</navigation>
The fragments are just basic fragments with a text view inside. Nothing special here.
Any ideas on what is missing?
As far as I see from the code you posted there are some errors in your configuration:
The nav host fragment should be contained in activity_main.xml (or your main activity layout file, the main activity should be defined in your manifest file and it's the one responsible to load the first fragment displayed by the app
The launcher fragment layout should not contain a NavHostFragment itself, but it should have some views inside the frame layout (a textview displaying some text for example)
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#+id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/navigation_launcher" />
</androidx.constraintlayout.widget.ConstraintLayout>
- fragment_launcher.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_height="match_parent"
android:layout_width="match_parent">
<TextView android:text="I'm the launcher fragment"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</FrameLayout>
Android dev beginner here. I've followed a tutorial for fragment-based navigation. I'm running into troubles when changing device orientation. It works fine on a real device, but in the emulator it is broker.
I've read some things about the view being re-rendered on device orientation change, but in my case the entire layout gets messed up, including the top bar as well as the bottom bar (back, etc.). A screenshot and code included below.
Note: layout looks completely fine when seeing it in design tab of XML layout file.
This is what it looks like in a vertical orientation:
Code for main activity and main fragment is below:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
and my main fragment is this one:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainFragment"
android:id="#+id/parentLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="30dp"
android:gravity="center">
...some stuff here...
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
=> this looks incorrect:
android:layout_width="0dp"
android:layout_height="0dp"
Try to follow this example :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.example.news.ArticleListFragment"
android:id="#+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>