我想通过Payment
安全参数将我创建的 Parcelable 类从一个片段传递到另一个片段,但它可以为空。
我创建了类型的参数com.example.Payment
并使其可以为空。当我编译应用程序时,我在 build 文件夹下收到一条错误Expression in a class literal has a nullable type 'Payment?', use !! to make the type non-nullable
消息FragmentDirection
。
错误发生在这一行:
@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
}
//...
}
我不知道我做错了什么并且无法编辑文件,因为它在 build 文件夹下。
编辑:有导航的xml代码:
<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>