Question
Android Navigation - Define Argument
I have some problems, when I define an argument in fragment, the letters in name Attribute of Argument display in red color but I see green tick in top XML file that means no problems found. My Android Studio is Giraffe version.
The message of this error is : 'firstName' is not a valid destination for tag 'argument'. I tried for other words but each time I have this error. I compiled and ran it on the emulator and everything looked good and it runs correctly.
I updated my Android Studio to Giraffe to solve repository problems of navigation and now I have this problem. It seems a problem with Android Studio.
build.gradle.kts (Project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
}
dependencies {
val nav_version = "2.6.0"
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version")
}
}
plugins {
id("com.android.application") version "8.1.0" apply false
id("org.jetbrains.kotlin.android") version "1.8.0" apply false
}
build.gradle.kts (App)
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
//id("androidx.navigation.safeargs")
id("androidx.navigation.safeargs.kotlin")
}
android {
namespace = "com.example.e12s02_navigationcomponent"
compileSdk = 33
defaultConfig {
applicationId = "com.example.e12s02_navigationcomponent"
minSdk = 24
targetSdk = 33
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation("androidx.core:core-ktx:1.10.1")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.9.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
//Navigation Component
implementation("androidx.navigation:navigation-fragment-ktx:2.6.0")
implementation("androidx.navigation:navigation-ui-ktx:2.6.0")
}
main_nav.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/main_nav_graph"
app:startDestination="@id/firstFragment">
<fragment
android:id="@+id/firstFragment"
android:name="com.example.e12s02_navigationcomponent.FirstFragment"
android:label="fragment_first"
tools:layout="@layout/fragment_first" >
<action
android:id="@+id/action_firstFragment_to_secondFragment"
app:destination="@id/secondFragment" />
</fragment>
<fragment
android:id="@+id/secondFragment"
android:name="com.example.e12s02_navigationcomponent.SecondFragment"
android:label="fragment_second"
tools:layout="@layout/fragment_second" >
<action
android:id="@+id/action_secondFragment_to_firstFragment"
app:destination="@id/firstFragment" />
<argument
android:name="firstName"
app:argType="string" />
</fragment>
</navigation>