java.lang.IllegalStateException: Method addObserver must be called on the main thread

未关闭
#879 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
需要澄清
活跃度
停滞
技术栈
android, kotlin

调研方向

从 SensorsTests.userSensorsAreVisible 和 launchFragmentInHiltContainer helper 开始,重点检查 ActivityScenario.onActivity 和 navController.setGraph 调用。重现集成测试并追踪报告的主线程异常;完成标准是得到一个有文档记录且可复现的解决方案,使 scenario 能够在不出现该异常的情况下运行。

由索引模型根据 Issue 内容生成。

描述

Hello, I am trying to perform integration testing and use launchFragmentInHiltContainer to start my fragment for specific scenario. However I receive and error java.lang.IllegalStateException: Method addObserver must be called on the main thread on the navController.setGraph(R.navigation.nav_graph) line. Below I have provided my test class and the launchFragmentInHiltContainer.

@HiltAndroidTest
class SensorsTests {

    @get:Rule
    var hiltRule = HiltAndroidRule(this)

    @Inject
    lateinit var repository: MockRepositoryImpl

    @BeforeAll
    fun beforeAll() {
        hiltRule.inject()
    }

    @Test
    fun userSensorsAreVisible() {
        val navController = TestNavHostController(ApplicationProvider.getApplicationContext())
        navController.setGraph(R.navigation.nav_graph)

        launchFragmentInHiltContainer<SensorsFragment>(navHostController = navController)

        Thread.sleep(10000)
    }

}
inline fun <reified T : Fragment> launchFragmentInHiltContainer(
    fragmentArgs: Bundle? = null,
    @StyleRes themeResId: Int = R.style.Theme_SensorsTracker,
    navHostController: NavHostController? = null,
    crossinline action: Fragment.() -> Unit = {}
) {
    val startActivityIntent = Intent.makeMainActivity(
        ComponentName(
            ApplicationProvider.getApplicationContext(),
            HiltTestActivity::class.java
        )
    ).putExtra(
        "androidx.fragment.app.testing.FragmentScenario.EmptyFragmentActivity.THEME_EXTRAS_BUNDLE_KEY",
        themeResId
    )

    ActivityScenario.launch<HiltTestActivity>(startActivityIntent).onActivity { activity ->
        val fragment: Fragment = activity.supportFragmentManager.fragmentFactory.instantiate(
            Preconditions.checkNotNull(T::class.java.classLoader),
            T::class.java.name
        )

        fragment.arguments = fragmentArgs

        fragment.viewLifecycleOwnerLiveData.observeForever { viewLifecycleOwner ->
            if (viewLifecycleOwner != null) {
                navHostController?.let {
                    Navigation.setViewNavController(fragment.requireView(), it)
                }
            }
        }

        activity.supportFragmentManager
            .beginTransaction()
            .add(android.R.id.content, fragment, "")
            .commitNow()

        fragment.action()
    }
}
主要语言
Kotlin
星标
45.9k
派生
11.8k
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

android/architecture-samples 的其他 Issue

查看 android/architecture-samples 的全部 Issue

相似的 Issue

更多 Kotlin Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。