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

Đang mở
#879 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
25/100
Loại issue
Lỗi
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
android, kotlin
Lĩnh vực
mobile-dev, testing

Hướng nghiên cứu

Bắt đầu với SensorsTests.userSensorsAreVisible và helper launchFragmentInHiltContainer, đặc biệt là ActivityScenario.onActivity và lệnh gọi navController.setGraph. Tái hiện integration test và lần theo exception trên main thread đã được báo cáo; công việc hoàn tất khi có một cách giải quyết được ghi lại và có thể tái hiện, cho phép chạy scenario mà không gặp exception đó.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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()
    }
}
Ngôn ngữ chính
Kotlin
Star
45.9k
Fork
11.8k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của android/architecture-samples

Tất cả issue của android/architecture-samples

Issue tương tự

Thêm issue về Kotlin

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.