【问题标题】:Android Espresso does not have a NavController set ErrorAndroid Espresso 没有 NavController 设置错误
【发布时间】:2020-06-22 13:13:36
【问题描述】:

我正在尝试在我的导航架构中测试一个片段,我的测试如下:

test.kt

@RunWith(AndroidJUnit4::class)
@MediumTest
internal class AddingAccountTest{

@get:Rule
var activityRule: ActivityTestRule<MainActivity>
        = ActivityTestRule(MainActivity::class.java)

@Before fun loadCorrespondingFragment(){

}

@Test fun checkThatAllFieldsInFormAreEmpty(){
    // Create a TestNavHostController
    val navController = TestNavHostController(ApplicationProvider.getApplicationContext())
    navController.setGraph(R.navigation.navigation_drawer_main)

    // Create a graphical FragmentScenario for the TitleScreen
    val titleScenario = launchFragmentInContainer<AddAccountFragment>(Bundle(), themeResId = R.style.Locky_Theme)

    // Set the NavController property on the fragment
    titleScenario.onFragment { fragment ->
        Navigation.setViewNavController(fragment.requireView(), navController)
    }
    onView(withId(R.id.Account_Name)).check(matches(isEnabled()))
}
}

但是当我运行它时,我收到以下错误:

java.lang.IllegalStateException: View androidx.constraintlayout.widget.ConstraintLayout{65be158 V.E...... ......I. 0,0-0,0 #7f0a0129 app:id/cl_layout} does not have a NavController set
at androidx.navigation.Navigation.findNavController(Navigation.java:84)
at androidx.navigation.fragment.NavHostFragment.findNavController(NavHostFragment.java:118)
at androidx.navigation.fragment.FragmentKt.findNavController(Fragment.kt:29)
at com.th3pl4gu3.locky_offline.ui.main.add.account.AddAccountFragment.observeBackStackEntryForLogoResult(AddAccountFragment.kt:89)
at com.th3pl4gu3.locky_offline.ui.main.add.account.AddAccountFragment.onViewCreated(AddAccountFragment.kt:72)

错误发生在我的第 89 行的 AddAccountFragment.kt 中,其中包含以下代码:

AccountFragment.kt

val navBackStackEntry = findNavController().getBackStackEntry(R.id.Fragment_Add_Account)

此代码用于获取backstack入口数据如下:

private fun observeBackStackEntryForLogoResult() {
    // After a configuration change or process death, the currentBackStackEntry
    // points to the dialog destination, so you must use getBackStackEntry()
    // with the specific ID of your destination to ensure we always
    // get the right NavBackStackEntry
    val navBackStackEntry = findNavController().getBackStackEntry(R.id.Fragment_Add_Account)

    // Create our observer and add it to the NavBackStackEntry's lifecycle
    val observer = LifecycleEventObserver { _, event ->
        if (event == Lifecycle.Event.ON_RESUME
            && navBackStackEntry.savedStateHandle.contains(KEY_ACCOUNT_LOGO)
        ) {
            /*
            * Update the logo
            */
            viewModel.logoUrl =
                navBackStackEntry.savedStateHandle.get<String>(KEY_ACCOUNT_LOGO)!!

            navBackStackEntry.savedStateHandle.remove<AccountSort>(KEY_ACCOUNT_LOGO)
        }
    }
    navBackStackEntry.lifecycle.addObserver(observer)

    // As addObserver() does not automatically remove the observer, we
    // call removeObserver() manually when the view lifecycle is destroyed
    viewLifecycleOwner.lifecycle.addObserver(LifecycleEventObserver { _, event ->
        if (event == Lifecycle.Event.ON_DESTROY) {
            navBackStackEntry.lifecycle.removeObserver(observer)
        }
    })
}

有人可以帮我解释为什么会发生这个错误吗?

【问题讨论】:

标签: android-espresso android-testing android-architecture-navigation android-jetpack-navigation android-espresso-recorder


【解决方案1】:
titleScenario.onFragment { fragment ->
    Navigation.setViewNavController(fragment.requireView(), navController)
}

onFragment 在片段移动到恢复状态后调用。为时已晚,因为您在 onCreateView 中使用 NavController(根据您发布的堆栈跟踪)。

要解决这个问题,你需要尽快设置TestNavHostController 方式,就在片段视图创建之后:

val titleScenario = launchFragmentInContainer<AddAccountFragment>(
    Bundle(),
    themeResId = R.style.Locky_Theme
).also { fragment ->
    fragment.viewLifecycleOwnerLiveData.observeForever { viewLifecycleOwner ->
        if (viewLifecycleOwner != null) {
            Navigation.setViewNavController(fragment.requireView(), navController)
        }
    }
}

要了解更多信息,请访问Test Navigation 指南。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2018-11-03
    • 2020-01-01
    • 1970-01-01
    • 2015-03-16
    相关资源
    最近更新 更多