首页 > 解决方案 > Android - Jetpack Compose - ResourceNotFoundException

问题描述

构建.gradle:

testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.4-alpha03")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.0-alpha03")
androidTestImplementation("androidx.compose.ui:ui-test-junit4:1.1.0-alpha06")

我的 androidTest 类:

@get:Rule
val composeTestRule = createAndroidComposeRule(MainActivity::class.java)

@Test
fun testUI() {
    composeTestRule.setContent {
        AppTheme {
            SomeComposeUI(
                
            )
        }
    }
    composeTestRule.activity.getString(R.string.apply)
    InstrumentationRegistry.getInstrumentation().context.getString(R.string.apply)
    
}

我遇到的问题是composeTestRule.activity.getString(R.string.apply)或者InstrumentationRegistry.getInstrumentation().context.getString(R.string.apply)总是返回Method threw 'android.content.res.Resources$NotFoundException'异常。

我尝试了最新的 alpha 版本和最新的稳定版本,但没有运气。我什至尝试过你可以在 android studio 中导入的 compose 项目,并且有同样的错误。

笔记:

任何帮助,将不胜感激

标签: androidandroid-jetpack-compose

解决方案


composeTestRule.activity.getString(R.string.apply)事实证明,在大多数情况下附加调试器时,我的项目和 android 项目无法使用。

在没有调试器的情况下运行不会有同样的问题,因此使开发变得更加困难,但至少我有一个修复。


推荐阅读