gpt4 book ai didi

android-fragments - java.lang.IllegalStateException : Can not perform this action after onSaveInstanceState caused by launchFragmentInContainer

转载 作者:行者123 更新时间:2023-12-03 15:58:41 24 4
gpt4 key购买 nike

我正在尝试测试应用程序的工作流程。导航组件已用于定义应用程序工作流程。已使用FragmentScenario根据此引用(https://developer.android.com/guide/navigation/navigation-testing)测试从一个片段到另一个片段的导航。

在build.gradle中添加了以下依赖项debugImplementation("androidx.fragment:fragment-testing:1.1.0-beta01") {
exclude group: 'androidx.test', module: 'core'
}

用于访问api launchFragmentInContainer
使用MockK模拟navController

以下是示例代码段

@RelaxedMockK
private lateinit var navController: NavController

@Before
fun setup() {
MockKAnnotations.init(this)
}

@Test
fun navigationToSecondFragmentTest() {
val secondFragmentScenario = launchFragmentInContainer<SecondFragment>()

secondFragmentScenario.onFragment {
Navigation.setViewNavController(it.requireView(), navController)
}
onView(ViewMatchers.withId(R.id.btn)).perform(ViewActions.click())
verify{
navController.navigate(R.id.secondFragment)
}
}

我的期望是通过测试用例,但出现以下运行时错误
 `java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState`

最佳答案

确保运行测试的设备已解锁。如果屏幕关闭或处于锁定屏幕,您将获得一个大致如下所示的堆栈跟踪:

java.lang.RuntimeException: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at androidx.test.runner.MonitoringInstrumentation.runOnMainSync(MonitoringInstrumentation.java:441)
at androidx.test.core.app.ActivityScenario.onActivity(ActivityScenario.java:564)
at androidx.fragment.app.testing.FragmentScenario.internalLaunch(FragmentScenario.java:300)
at androidx.fragment.app.testing.FragmentScenario.launchInContainer(FragmentScenario.java:282)
at com.foo.package.YourFragmentTest.yourTestFunction(YourFragmentTest.kt:xy)

Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

关于android-fragments - java.lang.IllegalStateException : Can not perform this action after onSaveInstanceState caused by launchFragmentInContainer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57344170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com