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

オープン
#879 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
android, kotlin

調査の方向性

SensorsTests.userSensorsAreVisible と launchFragmentInHiltContainer ヘルパーから始め、特に ActivityScenario.onActivity と navController.setGraph の呼び出しを確認します。統合テストを再現し、報告されたメインスレッド例外を追跡します。シナリオをその例外なしで実行できる、文書化された再現可能な解決策が得られれば完了です。

索引モデルが 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 はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

android/architecture-samples のほかの issue

android/architecture-samples の issue をすべて見る

似ている issue

Kotlin の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。