Android 导航组件:parcelable参数但获取错误类型不匹配:推断类型为Student,但应为字符串
在我的root build.gradle中,我有Android 导航组件:parcelable参数但获取错误类型不匹配:推断类型为Student,但应为字符串,android,kotlin,android-navigation,android-architecture-navigation,kotlin-android-extensions,Android,Kotlin,Android Navigation,Android Architecture Navigation,Kotlin Android Extensions,在我的root build.gradle中,我有导航安全参数gradle插件版本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.navigatio
导航安全参数gradle插件版本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>
在我的片段中,我做到了:
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>
<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>
MyDetailsFragment
<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>
<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>