user5436326
user5436326

Reputation:

Android resource linking failed activity_main.xml:19: error: attribute android:defaultNavHost not found after adding navigation components

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"
  />

after this log shows errorenter image description here

Upvotes: 4

Views: 12061

Answers (2)

AndroidGuy
AndroidGuy

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:

  1. In Project build.gradle :

    dependencies { classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.2.1" }

  2. 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

Wubbalubbadubdub
Wubbalubbadubdub

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 hostenter image description here, hope it does answer your question.

Upvotes: 7

Related Questions