Navigation argument Double error : Can't escape identifier `0.0` because it contains illegal characters

Navigation argument Double error : Can't escape identifier `0.0` because it contains illegal characters

在导航图参数中为 Double 设置默认值时出错。

 <argument
            android:name="lat"
            app:argType="java.lang.Double"
            android:defaultValue="0.0"
            />
        <argument
            android:name="lon"
            app:argType="kotlin.Double"
            android:defaultValue="0.0"
            />

我已经用安全参数插件尝试了 java.lang.Doublekotlin.Double

如何传递具有默认值的 Double 参数?

Safe Args 目前不支持小数类型。如果您不需要高精度但想通过十进制值发送(您可以找到 float 和 decimal 之间的差异 here), you should just use a float. You can find supported argument types here.

如果您确实需要高精度,则需要采取一些变通方法,例如将其作为字符串发送或将整数和小数部分分解为两个整数值(因此对于 12.43,您将发送 12 作为一个变量和 43 作为另一个)。

您可以创建一个可序列化 class 并在其中存储经纬度和经纬度,并在导航参数中将 class 的对象作为“自定义可序列化”类型传递。

这是一个示例,

科特林

data class LatLong(
    var lat: Double = 0.0,
    var long: Double = 0.0
) : Serializable

导航图:

<argument
        android:name="LatLong"
        app:argType="com.example.models.LatLong" />