gpt4 book ai didi

android - 导航组件 : parcelable argument but get error Type mismatch: inferred type is Student but String was expected

转载 作者:行者123 更新时间:2023-11-29 22:35:10 26 4
gpt4 key购买 nike

在我的根 build.gradle 中,我有 navigation-safe-args-gradle-plugin 版本 2.2.0-rc03:

script{
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.2.0-rc03'
}
}

在我的导航图 xml 中:

<fragment
android:id="@+id/MyListFragment"
android:name="com.foo.bar.ui.mylist.MyListFragment"
android:label="My List">

<action
android:id="@+id/action_myListFragment_to_myDetailFragment"
app:destination="@id/myDetailFragment" />

<!--Here I pass parcelable type as argument-->
<argument
android:name="Student"
app:argType="com.foo.core.model.Student"/>

</fragment>

Student 是一个普通的类。

package com.foo.core.model

@Parcelize
data class Student(val studentNumber: Int): Parcelable

在我的 fragment 中我这样做:

val student:Student=Student(123)
findNavController().navigate(actionMyListFragmentToMyDetailFragment(student))

当我构建我的项目时,出现编译器错误:

Type mismatch: inferred type is Student but String was expected

这是为什么?

最佳答案

将您的参数放在MyDetailsFragment中而不是MyListFragment

MyListFragment

<fragment
android:id="@+id/MyListFragment"
android:name="com.foo.bar.ui.mylist.MyListFragment"
android:label="My List">

<action
android:id="@+id/action_myListFragment_to_myDetailFragment"
app:destination="@id/MyDetailsFragment" />

</fragment>

MyDetailsFragment

<fragment
android:id="@+id/MyDetailsFragment"
android:name="com.foo.bar.ui.mylist.MyDetailsFragment"
android:label="My Details">

<argument
android:name="Student"
app:argType="com.foo.core.model.Student"/>

</fragment>

关于android - 导航组件 : parcelable argument but get error Type mismatch: inferred type is Student but String was expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59553118/

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