未解决的参考Android Safeargs
我似乎无法让Safeargs工作:
我的XML导航文件包含:
<fragment
android:id="@+id/nav_adv_list"
android:name="com.example.drawerexample.ui.AdvListFragment"
android:label="@string/menu_show_adv_list"
tools:layout="@layout/fragment_adv_list" >
<argument
android:name="onlyUserAdvs"
app:argType="boolean"
android:defaultValue="true" />
...
<action
android:id="@+id/nav_adv_list_to_my_adv_list"
android:name="com.example.drawerexample.ui.AdvListFragment"
app:destination="@id/nav_adv_list">
<argument
android:name="onlyUserAdvs"
app:argType="boolean"
android:defaultValue="true" />
</action>
</fragment>
这是因为我需要参数为false
默认情况下,而true
到来自特定的片段。 无论如何,我的kotlin代码如下:
class AdvListFragment : Fragment(), DatePickerDialog.OnDateSetListener {
...
private val fragmentArgs : AdvListFragmentArgs by navArgs()
fun useArgs() {
val x = arguments.onlyUserAdvs
}
}
这很好,因为在编写代码时,Android Studio识别绑定,甚至暗示了(唯一的)变量存在。但是,当构建应用程序时,我会得到:
.../ui/AdvListFragment.kt: (54, 63): Unresolved reference: onlyUserAdvs
我已经阅读了多次文档,所以我认为我的gradle还可以,但是无论如何,这是项目级别的build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
}
dependencies {
classpath 'com.google.gms:google-services:4.3.10'
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.4.2")
}
}
plugins {
id 'com.android.application' version '7.2.0' apply false
id 'com.android.library' version '7.2.0' apply false
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
App Level Gradle.build:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android'
id 'kotlin-kapt'
}
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'androidx.navigation.safeargs.kotlin'
android {
compileSdk 32
defaultConfig {
applicationId "com.example.drawerexample"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled 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'
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'
implementation "androidx.navigation:navigation-fragment-ktx:2.4.2"
implementation "androidx.navigation:navigation-ui-ktx:2.4.2"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation platform('com.google.firebase:firebase-bom:30.0.0')
implementation 'com.google.firebase:firebase-analytics-ktx:21.0.0'
implementation 'com.google.android.gms:play-services-auth:20.2.0'
implementation 'com.google.firebase:firebase-firestore-ktx:24.1.2'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.google.firebase:firebase-auth-ktx'
implementation 'com.google.firebase:firebase-storage-ktx'
}
我也尝试重新开放Android Studio重新开放了该项目,清洁项目并没有运气来重建项目。任何帮助都将受到赞赏。
I can't seem to be able to get SafeArgs to work:
My XML navigation file contains:
<fragment
android:id="@+id/nav_adv_list"
android:name="com.example.drawerexample.ui.AdvListFragment"
android:label="@string/menu_show_adv_list"
tools:layout="@layout/fragment_adv_list" >
<argument
android:name="onlyUserAdvs"
app:argType="boolean"
android:defaultValue="true" />
...
<action
android:id="@+id/nav_adv_list_to_my_adv_list"
android:name="com.example.drawerexample.ui.AdvListFragment"
app:destination="@id/nav_adv_list">
<argument
android:name="onlyUserAdvs"
app:argType="boolean"
android:defaultValue="true" />
</action>
</fragment>
This is because I need the argument to be false
by default and true
when coming from a specific fragment.
Anyway, my Kotlin code is the following:
class AdvListFragment : Fragment(), DatePickerDialog.OnDateSetListener {
...
private val fragmentArgs : AdvListFragmentArgs by navArgs()
fun useArgs() {
val x = arguments.onlyUserAdvs
}
}
Which is fine because when writing code Android Studio recognizes the binding and even suggests me the (only) variable present. However, when building the app I get:
.../ui/AdvListFragment.kt: (54, 63): Unresolved reference: onlyUserAdvs
I've read the documentation many times so I think my gradle is ok, but anyway here is the project level build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
}
dependencies {
classpath 'com.google.gms:google-services:4.3.10'
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.4.2")
}
}
plugins {
id 'com.android.application' version '7.2.0' apply false
id 'com.android.library' version '7.2.0' apply false
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
and the app level gradle.build:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android'
id 'kotlin-kapt'
}
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'androidx.navigation.safeargs.kotlin'
android {
compileSdk 32
defaultConfig {
applicationId "com.example.drawerexample"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled 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'
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'
implementation "androidx.navigation:navigation-fragment-ktx:2.4.2"
implementation "androidx.navigation:navigation-ui-ktx:2.4.2"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation platform('com.google.firebase:firebase-bom:30.0.0')
implementation 'com.google.firebase:firebase-analytics-ktx:21.0.0'
implementation 'com.google.android.gms:play-services-auth:20.2.0'
implementation 'com.google.firebase:firebase-firestore-ktx:24.1.2'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.google.firebase:firebase-auth-ktx'
implementation 'com.google.firebase:firebase-storage-ktx'
}
I've also tried reopening Android Studio, reopening the project, clean-building the project and rebuilding the project with no luck. Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改应用程序gradle.build
从
应用插件:'androidx.navigation.safeargs.kotlin'
我遇到
了同样的问题,这对我有帮助
change in the app gradle.build
from
apply plugin: 'androidx.navigation.safeargs.kotlin'
to
I had the same problem and it helped me
我只使用Kotlin,并且我还为所有组件和插件提供了导航版本2.4.1。
将版本更改为2.5.1帮助我。
I only use Kotlin and I also had navigation version 2.4.1 for all components and plugins.
Changing version to 2.5.1 helped me.