获取“ java.lang.IllegalArgumentException:目标ID == 0” aftel调用导航

问题描述

我得到了

java.lang.IllegalArgumentException: Destination id == 0 can only be used in conjunction with a valid navOptions.popUpTo

当我调用以下代码时:

override fun onNavigateItemClicked(rIdNavAppSectionItem: Int) {
        findNavController().navigate(rIdNavAppSectionItem)
    }

调试之后,我发现它正在使用rIdNavAppSectionItem == 0调用Navigation。

那么,我的 0 的来源是什么?

我正在从array.xml中读取资源的ID,我的所有get调用始终返回0。

我的res / values / array.xml是:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Notice that the android:id value does not include the plus sign in the ID reference,because the ID already exists,as defined in the ids.xml -->

    <integer-array name="main_nav_list">
        <item>@id/nav_weighing</item>
        <item>@id/nav_water_intake</item>
        <item>@id/nav_pill_remainder</item>
    </integer-array>

</resources>

我的res / values / ids.xml是:

<?xml version="1.0" encoding="utf-8"?>
<!-- https://developer.android.com/guide/topics/resources/more-resources#Id -->

<!-- Defines a unique ID. Takes no value,only attributes. -->
<resources>

    <item type="id" name="nav_home" />

    <item type="id" name="nav_weighing" />
    <item type="id" name="nav_water_intake" />
    <item type="id" name="nav_pill_remainder" />

    <item type="id" name="nav_weight_input" />
    <item type="id" name="nav_water_intake_input" />

</resources>

我的电话是这样的:

mRootView.context.resources.getIntArray(R.array.main_nav_list)

返回一个美丽的0数组!

您知道为什么这些ID没有初始化吗?

解决方法

我在写问题时可以解决我的问题:

我不知道为什么,但是在可绘制对象之前我也遇到了类似的错误。

并使用以下可绘制对象解决我过去的问题: Integer array of resource ids returns 0

所以我复制自己:

val navigations: TypedArray = mRootView.context.resources.getTypedArray(R.array.main_nav_list)

在for索引期间:

val itWantsToGetAnException: Int = 0
val navigate: Int = navigations.getResourceId(index,itWantsToGetAnException)

使用后:

navigations.recycle()

再也没有0了! :)

祝你有美好的一天!

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...