导航组件:可拆分的参数,但出现错误类型不匹配:推断的类型为Student,但需要字符串

问题描述 投票:1回答:1

在我的root 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

在我的片段中,我这样做:

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

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

Type mismatch: inferred type is Student but String was expected

为什么?

android kotlin android-navigation android-architecture-navigation kotlin-android-extensions
1个回答
0
投票

将您的argument放在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/myDetailFragment" />

</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>
© www.soinside.com 2019 - 2024. All rights reserved.