我试图在导航片段之间传递一个对象。我能够构建该项目,但是当它启动时,我在 nav_graph 上收到一条错误消息:“异常膨胀 nav_graph 第 20 行”。第 20 行是 nav_graph 上的参数行。我刚刚将@Parcelize 关键字添加到我试图传递并设置nav_graph 的类的顶部。我需要做其他事情吗?
团队班:
@Parcelize
public class Team {
@SerializedName("idTeam")
@Expose
private String idTeam;
@SerializedName("idSoccerXML")
@Expose
private String idSoccerXML;
@SerializedName("idAPIfootball")
@Expose
private String idAPIfootball;
@SerializedName("intLoved")
@Expose
private String intLoved;
@SerializedName("strTeam")
@Expose
private String strTeam;
@SerializedName("strTeamShort")
@Expose
private String strTeamShort;
导航图:
<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/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.jaykallen.searchapi.ui.HomeFragment"
android:label="HomeFragment">
<action
android:id="@+id/action_homeFragment_to_resultsFragment"
app:destination="@id/resultsFragment" />
</fragment>
<fragment
android:id="@+id/resultsFragment"
android:name="com.jaykallen.searchapi.ui.ResultsFragment"
android:label="ResultsFragment">
<argument
android:name="choice"
app:argType="com.jaykallen.searchapi.model.Team"
app:nullable="true" />
</fragment>
</navigation>
HomeFragment方法:
private fun choiceClicked(chosen: Team) {
println("User clicked: ${chosen.strTeam}")
homeViewModel.choice = chosen
val action = HomeFragmentDirections.actionHomeFragmentToResultsFragment(chosen)
Navigation.findNavController(view!!).navigate(action)
}
结果片段方法:
private fun getSafeArgs() {
arguments?.let {
val args = ResultsFragmentArgs.fromBundle(it)
teamChosen = args.choice
if (teamChosen != null) {
println("Safe Argument Received=${teamChosen?.strTeam}")
updateUi(teamChosen)
}
}
}