I want to pass a Parcelable class Payment
, that I created, from one fragment to another with safe args, but it can be null.
I created the argument of type com.example.Payment
and make it nullable. When I compile the app I get an error saying Expression in a class literal has a nullable type 'Payment?', use !! to make the type non-nullable
in FragmentDirection
, under the build folder.
The error happended in this line :
@Suppress("CAST_NEVER_SUCCEEDS")
class FragmentDirections private constructor(
{
private data class GoToNew(
val Payment: Payment?
) : NavDirections {
override fun getArguments(): Bundle {
val result = Bundle()
if (Parcelable::class.java.isAssignableFrom(Payment::class.java)) { // Error happend here, when trying to get the class of Payment
result.putParcelable("Payment", this.Payment as Parcelable?)
} else if (Serializable::class.java.isAssignableFrom(Payment::class.java)) { // There is also the same error here
result.putSerializable("Payment", this.Payment as Serializable?)
} else {
throw UnsupportedOperationException(Payment::class.java.name +
" must implement Parcelable or Serializable or must be an Enum.") // and here
}
return result
}
//...
}
I don't know what I did wrong and can't edit the file because its under build folder.
EDIT : There is the xml code of the navigation :
<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/main_navigation"
app:startDestination="@id/listFragment">
<fragment
android:id="@+id/newPaymentFragment"
android:name="com.example.wallet.application.newPayment.NewPaymentFragment"
android:label="fragment_new_payment"
tools:layout="@layout/fragment_new_payment" >
<argument android:name="Payment"
app:argType="com.example.Payment"
app:nullable="true"/>
<action
android:id="@+id/backToList"
app:destination="@id/listFragment" />
<action
android:id="@+id/goToDetail"
app:destination="@id/detailFragment" />
</fragment>
<!-- ... -->
</navigation>
In module level gradle file change
apply plugin: "androidx.navigation.safeargs.kotlin"
toapply plugin: "androidx.navigation.safeargs"
. I have tried this one and its working fine now.