Issue
I'm trying to implement the Safe Args plugin, but my auto-generated class/method MainScreenFragmentDirections.actionMainScreenFragmentToNoteScreenFragment()
can't seem to find the specified arguments from the nav_graph. I don't think the problem is with the plugin not generating the classes, since I'm able to reference the necessary class and method - my issue is with the action method not recognizing my argument and giving me a warning:
Too many arguments for public final fun actionMainScreenFragmentToNoteScreenFragment(): NavDirections defined in com.example.notelite.ui.mainscreen.MainScreenFragmentDirections
I've applied all necessary plugins and dependencies on gradle.
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
id 'kotlin-parcelize'
id 'androidx.navigation.safeargs.kotlin'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.notelite"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
buildFeatures {
viewBinding true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
//Navigation Components
def nav_version = "2.3.5"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
// Feature module Support
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
// Testing Navigation
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1"
//Room Database
def room_version = "2.3.0"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
//Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"
//Test helpers
testImplementation "androidx.room:room-testing:$room_version"
//Dagger Hilt - Dependency Injection
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"
//Preferences DataStore
implementation "androidx.datastore:datastore-preferences:1.0.0-beta01"
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.4.30'
ext.hilt_version = '2.35'
repositories {
google()
jcenter()
}
dependencies {
def nav_version = "2.3.5"
classpath 'com.android.tools.build:gradle:4.2.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Here's my nav_graph code:
<?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/mainScreenFragment">
<fragment
android:id="@+id/mainScreenFragment"
android:name="com.example.notelite.ui.mainscreen.MainScreenFragment"
android:label="MainScreenFragment"
tools:layout="@layout/fragment_mainscreen">
<action
android:id="@+id/action_mainScreenFragment_to_noteScreenFragment"
app:destination="@id/noteScreenFragment" />
<argument android:name="noteid"
app:argType="integer"
android:defaultValue="0" />
</fragment>
<fragment
android:id="@+id/noteScreenFragment"
android:name="com.example.notelite.ui.notescreen.NoteScreenFragment"
android:label="NoteScreenFragment"
tools:layout="@layout/fragment_note_screen">
<action
android:id="@+id/action_noteScreenFragment_to_noteEditScreenFragment"
app:destination="@id/noteEditScreenFragment" />
</fragment>
<fragment
android:id="@+id/noteEditScreenFragment"
android:name="com.example.notelite.ui.notescreen.NoteEditScreenFragment"
android:label="NoteEditScreenFragment"
tools:layout="@layout/fragment_edit_note_screen"/>
</navigation>
Here's the code snippet I'm trying to call the method in - just defining a navigation action and trying to pass an argument parameter:
override fun onNoteClick(note: NoteEntity) {
val noteid = note.id
val action = MainScreenFragmentDirections.actionMainScreenFragmentToNoteScreenFragment(noteid)
}
"noteid" is the key value of an item in a Room database table. I've specified the type accordingly. Any ideas? Couldn't find anyone with a similar problem. I've tried clean and rebuild, restarting Android Studio, updating some plugins, updating IDE...nothing worked. What's funny is I can't seem to find the generated classes on the java(generated) folder, even though I'm able to reference them on my fragment - but that might be just me being a newbie and just really not finding them
Solution
Your <argument>
needs to be associated with the <fragment>
destination you are navigating to: your NoteScreenFragment
:
<?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/mainScreenFragment">
<fragment
android:id="@+id/mainScreenFragment"
android:name="com.example.notelite.ui.mainscreen.MainScreenFragment"
android:label="MainScreenFragment"
tools:layout="@layout/fragment_mainscreen">
<action
android:id="@+id/action_mainScreenFragment_to_noteScreenFragment"
app:destination="@id/noteScreenFragment" />
</fragment>
<fragment
android:id="@+id/noteScreenFragment"
android:name="com.example.notelite.ui.notescreen.NoteScreenFragment"
android:label="NoteScreenFragment"
tools:layout="@layout/fragment_note_screen">
<argument android:name="noteid"
app:argType="integer"
android:defaultValue="0" />
<action
android:id="@+id/action_noteScreenFragment_to_noteEditScreenFragment"
app:destination="@id/noteEditScreenFragment" />
</fragment>
<fragment
android:id="@+id/noteEditScreenFragment"
android:name="com.example.notelite.ui.notescreen.NoteEditScreenFragment"
android:label="NoteEditScreenFragment"
tools:layout="@layout/fragment_edit_note_screen"/>
</navigation>
Answered By - ianhanniballake
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.