Reputation:
Recently I have been pretty much excited about new exciting android tools. Among them Navigation literialy hooked me up. I thought this code lab would be wonderful to get started. Nonetheless, as an absolute beginner I found it a bit intriguing as it didn't state anything about step-by-step of setting up navigation components. So I started to break it up into bite sized section and start implementing it into a separate project.
My main activity code
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
}
override fun onSupportNavigateUp(): Boolean =
findNavController(R.id.my_nav_host_fragment).navigateUp()
}
in my main activity layout
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:navGraph="@navigation/mobile_navigation"
android:defaultNavHost="true"
/>
Upvotes: 4
Views: 12061
Reputation: 1330
In case you have already provided correct attributes; ie;
app:navGraph="@navigation/mobile_navigation"
app:defaultNavHost="true"
& you are still getting this error, follow below steps:
In Project build.gradle :
dependencies { classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.2.1" }
In app build.gradle :
def nav_version = "2.2.2"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
Upvotes: 2
Reputation: 2485
Your error is explicitly self-explanative
Android resource linking failed activity_main.xml:19: error: attribute android:defaultNavHost not found
The reason is You wrote the wrong attribute android:navGraph="@navigation/mobile_navigation"
android:defaultNavHost="true"
which should be app:navGraph="@navigation/mobile_navigation"
app:defaultNavHost="true"
if you edit these attributes you will see your project build successfully and in your navigation graph file shows as the default navigation host, hope it does answer your question.
Upvotes: 7