首页 > 解决方案 > 带有参数或操作的目标必须具有“名称”或“标识”属性

问题描述

我使用 Android 导航组件创建了一个全局操作,编译代码时出现以下错误

<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"
app:startDestination="@id/homeEnumerationFragment">

<fragment
    android:id="@+id/homeEnumerationFragment"
    android:name="org.southasia.ghru.ui.homeenumeration.HomeEnumerationFragment"
    android:label="HomeEnumerationFragment"
    tools:layout="@layout/home_enumeration_fragment"></fragment>


<fragment
    android:id="@+id/stationFragments"
    android:name="org.southasia.ghru.ui.station.StationFragment"
    android:label="StationFragment"
    tools:layout="@layout/station_fragment"/>

<fragment
    android:id="@+id/devicesFragment"
    android:name="org.southasia.ghru.ui.devices.DevicesFragment"
    android:label="DevicesFragment"
    tools:layout="@layout/devices_fragment"/>
<action android:id="@+id/action_global_stationFragments3" app:destination="@+id/stationFragments"/>

错误:

Error: Destination with arguments or actions must have 'name' or 'id' attributes.

标签: androidandroid-architecture-componentsandroid-architecture-navigation

解决方案


要使用全局操作,您需要为导航图提供一个 id(目前它不是自动生成的)。只需将 'id' 属性添加到您的导航元素,如下所示:

<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"
   app:startDestination="@id/homeEnumerationFragment"
   android:id="@+id/main">

推荐阅读