gpt4 book ai didi

android - fragmentDirections.action() 方法找不到在 nav_graph 上指定的参数

转载 作者:行者123 更新时间:2023-12-04 23:57:32 26 4
gpt4 key购买 nike

我正在尝试实现 Safe Args 插件,但我自动生成的类/方法 MainScreenFragmentDirections.actionMainScreenFragmentToNoteScreenFragment() 似乎无法从 nav_graph 中找到指定的参数。我认为问题不在于插件未生成类,因为我能够引用必要的类和方法 - 我的问题在于操作方法无法识别我的参数并给我警告:

public final fun actionMainScreenFragmentToNoteScreenFragment() 的参数太多:com.example.notelite.ui.mainscreen.MainScreenFragmentDirections 中定义的 NavDirections

我已经在 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
}

这是我的 nav_graph 代码:

<?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>

这是我尝试在其中调用方法的代码 fragment - 仅定义导航操作并尝试传递参数:

override fun onNoteClick(note: NoteEntity) {
val noteid = note.id
val action = MainScreenFragmentDirections.actionMainScreenFragmentToNoteScreenFragment(noteid)
}

“noteid”是 Room 数据库表中项目的键值。我已经相应地指定了类型。有任何想法吗?找不到有类似问题的人。我试过清理和重建、重启 Android Studio、更新一些插件、更新 IDE……没有任何效果。有趣的是,我似乎无法在 java(generated) 文件夹中找到生成的类,即使我能够在我的 fragment 中引用它们——但这可能只是我是一个新手,真的没有找到它们

最佳答案

你的 <argument>需要与 <fragment> 相关联您正在导航的目的地:您的 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>

关于android - fragmentDirections.action() 方法找不到在 nav_graph 上指定的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67491049/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com