Jay
Jay

Reputation: 634

Exception Inflating nav_graph when passing Parcelable object between Navigation Fragments

I'm attempting to pass an object between navigation fragments. I'm able to build the project, but when it launches, I get an error on the nav_graph stating: "Exception inflating nav_graph line 20". Line 20 is the argument line on the nav_graph. I just added the @Parcelize keyword to the top of the class I'm trying to pass and setup the nav_graph. Do I need to do something else?

Team Class:

@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;

Nav_Graph:

<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 Method:

private fun choiceClicked(chosen: Team) {
    println("User clicked: ${chosen.strTeam}")
    homeViewModel.choice = chosen
    val action = HomeFragmentDirections.actionHomeFragmentToResultsFragment(chosen)
    Navigation.findNavController(view!!).navigate(action)
}

ResultsFragment Method:

private fun getSafeArgs() {
    arguments?.let {
        val args = ResultsFragmentArgs.fromBundle(it)
        teamChosen = args.choice
        if (teamChosen != null) {
            println("Safe Argument Received=${teamChosen?.strTeam}")
            updateUi(teamChosen)
        }
    }
}

Upvotes: 3

Views: 672

Answers (1)

Ryan Simon
Ryan Simon

Reputation: 345

It turns out, all you needed to do is implement the Parcelable interface on your Java object. Normally, if you were using Kotlin, the @Parcelize annotation wouldn't have allowed you to compile without the Parcelable interface. It seems this compile time protection doesn't work for Java code.

By using Java objects, you'll also lose out on all of the automatic code generation goodies that come with the @Parcelize annotation.

In other words, I recommend you convert your Java file to Kotlin if you would like to take advantage of the @Parcelize annotation.

Upvotes: 1

Related Questions