【问题标题】:Android Navigation Component's Argument Value cannot take in String Resource?Android导航组件的参数值不能接受字符串资源?
【发布时间】:2022-08-17 21:46:27
【问题描述】:

我的navigation.xml 有以下代码

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<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/mobile_navigation\"
    app:startDestination=\"@+id/navigation_home\">

    <fragment
        android:id=\"@+id/navigation_notifications\"
        android:name=\"com.elyeproj.bottombarfragmentsswitching.ContainerFragment\"
        android:label=\"@string/title_notifications\"
        tools:layout=\"@layout/fragment_container\" >
        <argument
            android:name=\"FragmentKey\"
            app:argType=\"string\"
            android:defaultValue=\"@string/title_notifications\" />
        <argument
            android:name=\"FragmentColor\"
            app:argType=\"string\"
            android:defaultValue=\"#00FFFF\" />
    </fragment>
</navigation>

其中@string/title_notifications 将返回Notifications

但是,它在android:defaultValue=\"@string/title_notifications\" 处出错

        <argument
            android:name=\"FragmentKey\"
            app:argType=\"string\"
            android:defaultValue=\"@string/title_notifications\" />

错误是

Caused by: org.xmlpull.v1.XmlPullParserException: unsupported value \'Notifications\' for string. You must use a \"reference\" type to reference other resources.

@string/title_notifications 肯定有 Notifications 的值,因为它在 fragment 标记中使用得很好。

如果我转换为

        <argument
            android:name=\"FragmentKey\"
            app:argType=\"string\"
            android:defaultValue=\"Notifications\" />

然后它编译得很好。

为什么argument标签不能使用String资源?

    标签: android android-architecture-navigation android-navigation-graph


    【解决方案1】:

    看起来我必须改用 reference 类型。我不能混合字符串和参考

            <argument
                android:name="FragmentKey"
                app:argType="reference"
                android:defaultValue="@string/title_notifications" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-11
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多