android - Robolectric 使用导航架构组件,Resources$NotFoundException : nav_graph

标签 android android-testing robolectric androidx android-architecture-navigation

我在 android 中运行 robolectric 测试时遇到了麻烦

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />

我正在使用导航架构组件 我正在查看通货膨胀异常

下面是我的错误日志

 android.view.InflateException: XML file       C:\Users\StudioProjects\app\build\intermediates\merged-not-compiled-resources\debug\layout\activity_main.xml line #-1 (sorry, not yet implemented): XML file C:\Users\StudioProjects\app\build\intermediates\merged-not-compiled-resources\debug\layout\activity_main.xml line #-1 (sorry, not yet implemented): Error inflating class fragment Caused by: android.view.InflateException: XML file C:\Users\jitendrakumar\StudioProjects\app\build\intermediates\merged-not-compiled-resources\debug\layout\activity_main.xml line #-1 (sorry, not yet implemented): Error inflating class fragment
        Caused by: android.content.res.Resources$NotFoundException: packagename:navigation/nav_graph
            at org.robolectric.shadows.ShadowLegacyAssetManager.loadXmlResourceParser(ShadowLegacyAssetManager.java:591)
            at org.robolectric.shadows.ShadowResources.loadXmlResourceParser(ShadowResources.java:205)
            at android.content.res.Resources.loadXmlResourceParser(Resources.java)
            at android.content.res.Resources.getXml(Resources.java:1206)
            at androidx.navigation.NavInflater.inflate(NavInflater.java:67)
            at androidx.navigation.NavController.setGraph(NavController.java:424)
            at androidx.navigation.NavController.setGraph(NavController.java:406)
            at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:226)
            at android.support.v4.app.Fragment.performCreate(Fragment.java:2414)

我已经启用了

android.useAndroidX=true
android.enableJetifier=true

最佳答案

在 navigation.xml 文件(或您的自定义 res/navigation/*.xml)中查看您的根元素 ID。 ID 必须是“@+id/nav_graph”。示例:

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

关于android - Robolectric 使用导航架构组件,Resources$NotFoundException : nav_graph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54087429/

相关文章:

java - onSensorChanged() 未被调用

android - 如何在 Android 中模拟 Kotlin 的 kotlinx.android.synthetic View

android - Robolectric:Resources$NotFoundException:带有 Android Gradle 插件 3 的字符串资源 ID

android - 设置 NumberPicker 默认值

android - 屏幕锁定时检测 NFC 标签

java - F 点与 X 成 90 度

Android SQLite 数据库单元测试

android - 如何为 TestClasses 中的 Activity 变量赋值

android - Mocking Volley Network 模拟网络错误

robolectric - Robolectric 2.2 错误